-
Notifications
You must be signed in to change notification settings - Fork 388
/
NetworkService.go
903 lines (768 loc) · 26.8 KB
/
NetworkService.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
902
903
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package bacnetip
import (
"bytes"
"fmt"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"math"
)
type RouterStatus uint8
const (
ROUTER_AVAILABLE RouterStatus = iota // normal
ROUTER_BUSY // router is busy
ROUTER_DISCONNECTED // could make a connection, but hasn't
ROUTER_UNREACHABLE // temporarily unreachable
)
func (r RouterStatus) String() string {
switch r {
case ROUTER_AVAILABLE:
return "ROUTER_AVAILABLE"
case ROUTER_BUSY:
return "ROUTER_BUSY"
case ROUTER_DISCONNECTED:
return "ROUTER_DISCONNECTED"
case ROUTER_UNREACHABLE:
return "ROUTER_UNREACHABLE"
default:
return "Unknown"
}
}
type RouterInfo struct {
snet *uint16
address Address
dnets map[*uint16]RouterStatus
}
func (r RouterInfo) String() string {
return fmt.Sprintf("%#q", r)
}
type RouterInfoCache struct {
routers map[*uint16]*RouterInfo // TODO: snet -> {Address: RouterInfo}
pathInfo map[*uint16]*RouterInfo // TODO: (snet, dnet) -> RouterInfo
}
func NewRouterInfoCache() *RouterInfoCache {
log.Debug().Msg("NewRouterInfoCache")
return &RouterInfoCache{
routers: map[*uint16]*RouterInfo{},
pathInfo: map[*uint16]*RouterInfo{},
}
}
func (n *RouterInfoCache) GetRouterInfo(*uint16, *uint16) *RouterInfo {
panic("not implemented yet")
return nil
}
func (n *RouterInfoCache) UpdateRouterInfo(*uint16, any, any) error {
panic("not implemented yet")
return nil
}
func (n *RouterInfoCache) UpdateRouterStatus(*uint16, *Address, []*uint16) {
panic("not implemented yet")
}
func (n *RouterInfoCache) DeleteRouterInfo(*uint16, any, any) error {
panic("not implemented yet")
return nil
}
func (n *RouterInfoCache) UpdateSourceNetwork() {
panic("not implemented yet")
}
// TODO: implement me
type NetworkAdapter struct {
*Client
adapterSAP *NetworkServiceAccessPoint
adapterNet *uint16
adapterAddr *Address
adapterNetConfigured *int
}
func NewNetworkAdapter(sap *NetworkServiceAccessPoint, net *uint16, addr *Address, cid *int) (*NetworkAdapter, error) {
n := &NetworkAdapter{
adapterSAP: sap,
adapterNet: net,
adapterAddr: addr,
}
var err error
n.Client, err = NewClient(cid, n)
if err != nil {
return nil, errors.Wrap(err, "error creating client")
}
// record if this was 0=learned, 1=configured, None=unknown
if net != nil {
var state = 1
n.adapterNetConfigured = &state
}
return n, nil
}
// Confirmation Decode upstream PDUs and pass them up to the service access point.
func (n *NetworkAdapter) Confirmation(npdu _PDU) error {
log.Debug().
Stringer("npdu", npdu).
Interface("adapterNet", n.adapterNet).
Msg("confirmation")
return n.adapterSAP.ProcessNPDU(n, npdu)
}
// ProcessNPDU Encode NPDUs from the service access point and send them downstream.
func (n *NetworkAdapter) ProcessNPDU(npdu _PDU) error {
log.Debug().
Stringer("npdu", npdu).
Interface("adapterNet", n.adapterNet).
Msg("ProcessNPDU")
return n.Request(npdu)
}
func (n *NetworkAdapter) EstablishConnectionToNetwork(net any) error {
panic("not implemented yet")
}
func (n *NetworkAdapter) DisconnectConnectionToNetwork(net any) error {
panic("not implemented yet")
}
type NetworkServiceAccessPoint struct {
*ServiceAccessPoint
*Server
adapters map[*uint16]*NetworkAdapter
routerInfoCache *RouterInfoCache
pendingNets map[*uint16][]_PDU
localAdapter *NetworkAdapter
}
func NewNetworkServiceAccessPoint(routerInfoCache *RouterInfoCache, sapID *int, sid *int) (*NetworkServiceAccessPoint, error) {
n := &NetworkServiceAccessPoint{}
var err error
n.ServiceAccessPoint, err = NewServiceAccessPoint(sapID, n)
if err != nil {
return nil, errors.Wrap(err, "error creating network service access point")
}
n.Server, err = NewServer(sid, n)
if err != nil {
return nil, errors.Wrap(err, "error creating server")
}
// map of directly connected networks
n.adapters = make(map[*uint16]*NetworkAdapter)
// use the provided cache or make a default one
if routerInfoCache == nil {
routerInfoCache = NewRouterInfoCache()
}
n.routerInfoCache = routerInfoCache
// map to a list of application layer packets waiting for a path
n.pendingNets = make(map[*uint16][]_PDU)
return n, nil
}
/*
bind creates a network adapter object and bind.
bind(s, None, None)
Called for simple applications, local network unknown, no specific
address, APDUs sent upstream
bind(s, net, None)
Called for routers, bind to the network, (optionally?) drop APDUs
bind(s, None, address)
Called for applications or routers, bind to the network (to be
discovered), send up APDUs with a metching address
bind(s, net, address)
Called for applications or routers, bind to the network, send up
APDUs with a metching address.
*/
func (n *NetworkServiceAccessPoint) bind(server _Server, net *uint16, address *Address) error {
log.Debug().
Interface("server", server).
Interface("net", net).
Stringer("address", address).
Msg("bind")
// make sure this hasn't already been called with this network
if _, ok := n.adapters[net]; ok {
return errors.Errorf("Allready bound: %v", net)
}
// create an adapter object, add it to our map
adapter, err := NewNetworkAdapter(n, net, address, nil)
if err != nil {
return errors.Wrap(err, "error creating adapter")
}
n.adapters[net] = adapter
log.Debug().
Interface("net", net).
Stringer("adapter", adapter).
Msg("adapter")
// if the address was given, make it the "local" one
if address != nil {
log.Debug().Msg("setting local adapter")
n.localAdapter = adapter
}
// if the local adapter isn't set yet, make it the first one, and can
// be overridden by a subsequent call if the address is specified
if n.localAdapter == nil {
log.Debug().Msg("default local adapter")
n.localAdapter = adapter
}
if n.localAdapter.adapterAddr == nil {
log.Debug().Msg("no local address")
}
return bind(adapter, server)
}
// UpdateRouterReference Update references to routers.
func (n *NetworkServiceAccessPoint) UpdateRouterReference(snet *uint16, address, dnets any) error {
log.Debug().
Interface("snet", snet).
Interface("address", address).
Interface("dnets", dnets).
Msg("UpdateRouterReference")
// see if we have an adapter for the snet
_, ok := n.adapters[snet]
if !ok {
return errors.Errorf("no adapter for network: %d", snet)
}
// pass this along to the cache
return n.routerInfoCache.UpdateRouterInfo(snet, address, dnets)
}
// DeleteRouterReference Delete references to routers/networks.
func (n *NetworkServiceAccessPoint) DeleteRouterReference(snet *uint16, address, dnets any) error {
log.Debug().
Interface("snet", snet).
Interface("address", address).
Interface("dnets", dnets).
Msg("NetworkServiceAccessPoint")
// see if we have an adapter for the snet
_, ok := n.adapters[snet]
if !ok {
return errors.Errorf("no adapter for network: %d", snet)
}
//pass this along to the cache
return n.routerInfoCache.DeleteRouterInfo(snet, address, dnets)
}
func (n *NetworkServiceAccessPoint) Indication(pdu _PDU) error {
log.Debug().Stringer("pdu", pdu).Msg("Indication")
// make sure our configuration is OK
if len(n.adapters) == 0 {
return errors.New("no adapters")
}
// get the local adapter
localAdapter := n.localAdapter
log.Debug().Stringer("localAdapter", localAdapter).Msg("localAdapter")
// get the apdu
apdu := pdu.GetMessage().(readWriteModel.APDU)
// build a npdu
pduDestination := pdu.GetPDUDestination()
// the hop count always starts out big
hopCount := uint8(0xff)
// if this is route aware, use it for the destination
if settings.RouteAware && pduDestination.AddrRoute != nil {
// always a local station for now, in theory this could also be
// a local broadcast address, remote station, or remote broadcast
// but that is not supported by the patterns
if pduDestination.AddrRoute.AddrType != LOCAL_STATION_ADDRESS {
panic("Route must be of type local station")
}
var dadr *Address
switch pduDestination.AddrType {
case REMOTE_STATION_ADDRESS, REMOTE_BROADCAST_ADDRESS, GLOBAL_BROADCAST_ADDRESS:
dadr = pduDestination.AddrRoute
}
pdu.SetPDUDestination(pduDestination.AddrRoute)
npdu, err := buildNPDU(hopCount, nil, dadr, pdu.GetExpectingReply(), pdu.GetNetworkPriority(), apdu)
if err != nil {
return errors.Wrap(err, "error building NPDU")
}
return localAdapter.ProcessNPDU(NewPDUFromPDUWithNewMessage(pdu, npdu))
}
// local stations given to local adapter
if pduDestination.AddrType == LOCAL_STATION_ADDRESS {
npdu, err := buildNPDU(hopCount, nil, nil, pdu.GetExpectingReply(), pdu.GetNetworkPriority(), apdu)
if err != nil {
return errors.Wrap(err, "error building NPDU")
}
return localAdapter.ProcessNPDU(NewPDUFromPDUWithNewMessage(pdu, npdu))
}
// local broadcast given to local adapter
if pduDestination.AddrType == LOCAL_BROADCAST_ADDRESS {
npdu, err := buildNPDU(hopCount, nil, nil, pdu.GetExpectingReply(), pdu.GetNetworkPriority(), apdu)
if err != nil {
return errors.Wrap(err, "error building NPDU")
}
return localAdapter.ProcessNPDU(NewPDUFromPDUWithNewMessage(pdu, npdu))
}
// global broadcast
if pduDestination.AddrType == GLOBAL_BROADCAST_ADDRESS {
pdu.SetPDUDestination(NewLocalBroadcast(nil))
npdu, err := buildNPDU(hopCount, nil, pduDestination, pdu.GetExpectingReply(), pdu.GetNetworkPriority(), apdu)
if err != nil {
return errors.Wrap(err, "error building NPDU")
}
// send it to all of connected adapters
for _, xadapter := range n.adapters {
if err := xadapter.ProcessNPDU(NewPDUFromPDUWithNewMessage(pdu, npdu)); err != nil {
return errors.Wrap(err, "error processing NPDU")
}
}
return nil
}
// remote broadcast
switch pduDestination.AddrType {
case REMOTE_BROADCAST_ADDRESS, REMOTE_STATION_ADDRESS:
default:
return errors.Errorf("invalid destination address type: %s", pduDestination.AddrType)
}
dnet := pduDestination.AddrNet
log.Debug().Interface("dnet", dnet).Msg("using dnet")
// if the network matches the local adapter it's local
if dnet == localAdapter.adapterNet {
switch pduDestination.AddrType {
case REMOTE_STATION_ADDRESS:
log.Debug().Msg("mapping remote station to local station")
localStation, err := NewLocalStation(pduDestination.AddrAddress, nil)
if err != nil {
return errors.Wrap(err, "error building local station")
}
pdu.SetPDUDestination(localStation)
case REMOTE_BROADCAST_ADDRESS:
pdu.SetPDUDestination(NewLocalBroadcast(nil))
default:
return errors.New("Addressing problem")
}
npdu, err := buildNPDU(hopCount, nil, pduDestination, pdu.GetExpectingReply(), pdu.GetNetworkPriority(), apdu)
if err != nil {
return errors.Wrap(err, "error building NPDU")
}
return localAdapter.ProcessNPDU(NewPDUFromPDUWithNewMessage(pdu, npdu))
}
// get it ready to send when the path is found
pdu.SetPDUDestination(nil)
npdu, err := buildNPDU(hopCount, nil, pduDestination, pdu.GetExpectingReply(), pdu.GetNetworkPriority(), apdu)
if err != nil {
return errors.Wrap(err, "error building NPDU")
}
// we might already be waiting for a path for this network
if pendingNet, ok := n.pendingNets[dnet]; ok {
log.Debug().Msg("already waiting for a path")
var pdu _PDU = NewPDUFromPDUWithNewMessage(pdu, npdu)
n.pendingNets[dnet] = append(pendingNet, pdu)
return nil
}
// look for routing information from the network of one of our adapters to the destination network
var routerInfo *RouterInfo
var snetAdapter *NetworkAdapter
for snet, adapter := range n.adapters {
routerInfo = n.routerInfoCache.GetRouterInfo(snet, dnet)
if routerInfo != nil {
snetAdapter = adapter
break
}
}
// if there is info, we have a path
if routerInfo != nil {
log.Debug().Stringer("routerInfo", routerInfo).Msg("routerInfo found")
// check the path status
dnetStatus := routerInfo.dnets[dnet]
log.Debug().Stringer("dnetStatus", dnetStatus).Msg("dnetStatus")
// fix the destination
pdu.SetPDUDestination(&routerInfo.address)
// send it along
return snetAdapter.ProcessNPDU(NewPDUFromPDUWithNewMessage(pdu, npdu))
} else {
log.Debug().Msg("no known path to network")
// add it to the list of packets waiting for the network
netList := append(n.pendingNets[dnet], NewPDUFromPDUWithNewMessage(pdu, npdu))
n.pendingNets[dnet] = netList
// build a request for the network and send it to all the adapters
whoIsRouterToNetwork := readWriteModel.NewNLMWhoIsRouterToNetwork(dnet, 0)
// send it to all the adapters
for _, adapter := range n.adapters {
if err := n.SapIndicationWithAdapter(adapter, NewPDU(whoIsRouterToNetwork, WithPDUDestination(NewLocalBroadcast(nil)))); err != nil {
return errors.Wrap(err, "error doing SapIndication")
}
}
}
panic("not implemented yet")
}
func buildNPDU(hopCount uint8, source *Address, destination *Address, expectingReply bool, networkPriority readWriteModel.NPDUNetworkPriority, apdu readWriteModel.APDU) (readWriteModel.NPDU, error) {
sourceSpecified := source != nil
var sourceNetworkAddress *uint16
var sourceLength *uint8
var sourceAddress []uint8
if sourceSpecified {
sourceSpecified = true
sourceNetworkAddress = source.AddrNet
sourceLengthValue := *source.AddrLen
if sourceLengthValue > math.MaxUint8 {
return nil, errors.New("source address length overflows")
}
sourceLengthValueUint8 := uint8(sourceLengthValue)
sourceLength = &sourceLengthValueUint8
sourceAddress = source.AddrAddress
if sourceLengthValueUint8 == 0 {
// If we define the len 0 we must not send the array
sourceAddress = nil
}
}
destinationSpecified := destination != nil
var destinationNetworkAddress *uint16
var destinationLength *uint8
var destinationAddress []uint8
var destinationHopCount *uint8
if destinationSpecified {
destinationSpecified = true
destinationNetworkAddress = destination.AddrNet
destinationLengthValue := *destination.AddrLen
if destinationLengthValue > math.MaxUint8 {
return nil, errors.New("source address length overflows")
}
destinationLengthValueUint8 := uint8(destinationLengthValue)
destinationLength = &destinationLengthValueUint8
destinationAddress = destination.AddrAddress
if destinationLengthValueUint8 == 0 {
// If we define the len 0 we must not send the array
destinationAddress = nil
}
destinationHopCount = &hopCount
}
control := readWriteModel.NewNPDUControl(false, destinationSpecified, sourceSpecified, expectingReply, networkPriority)
return readWriteModel.NewNPDU(1, control, destinationNetworkAddress, destinationLength, destinationAddress, sourceNetworkAddress, sourceLength, sourceAddress, destinationHopCount, nil, apdu, 0), nil
}
func (n *NetworkServiceAccessPoint) ProcessNPDU(adapter *NetworkAdapter, pdu _PDU) error {
log.Debug().
Stringer("adapter", adapter).
Stringer("pdu", pdu).
Msg("ProcessNPDU")
// make sure our configuration is OK
if len(n.adapters) == 0 {
return errors.New("no adapters")
}
var (
processLocally bool
forwardMessage bool
)
npdu := pdu.GetMessage().(readWriteModel.NPDU)
sourceAddress := &Address{AddrType: NULL_ADDRESS}
if npdu.GetControl().GetSourceSpecified() {
snet := npdu.GetSourceNetworkAddress()
sadr := npdu.GetSourceAddress()
var err error
sourceAddress, err = NewAddress(snet, sadr)
if err != nil {
return errors.Wrapf(err, "error parsing source address %x", sadr)
}
}
destinationAddress := &Address{AddrType: NULL_ADDRESS}
if npdu.GetControl().GetDestinationSpecified() {
dnet := npdu.GetDestinationNetworkAddress()
dadr := npdu.GetDestinationAddress()
var err error
destinationAddress, err = NewAddress(dnet, dadr)
if err != nil {
return errors.Wrapf(err, "error parsing destination address %x", dadr)
}
}
switch {
// check for source routing
case npdu.GetControl().GetSourceSpecified() && sourceAddress.AddrType != NULL_ADDRESS:
log.Debug().Msg("check source path")
// see if this is attempting to spoof a directly connected network
snet := npdu.GetSourceNetworkAddress()
if _, ok := n.adapters[snet]; !ok {
log.Warn().Msg("path error (1)")
return nil
}
// pass this new path along to the cache
n.routerInfoCache.UpdateRouterStatus(adapter.adapterNet, pdu.GetPDUSource(), []*uint16{snet})
// check for destination routing
case !npdu.GetControl().GetDestinationSpecified() || destinationAddress.AddrType == NULL_ADDRESS:
log.Debug().Msg("no DADR")
processLocally = adapter == n.localAdapter || npdu.GetControl().GetMessageTypeFieldPresent()
forwardMessage = false
case destinationAddress.AddrType == REMOTE_BROADCAST_ADDRESS:
log.Debug().Msg("DADR is remote broadcast")
if *destinationAddress.AddrNet == *adapter.adapterNet {
log.Warn().Msg("path error (2)")
return nil
}
processLocally = *destinationAddress.AddrNet == *n.localAdapter.adapterNet
forwardMessage = true
case destinationAddress.AddrType == REMOTE_STATION_ADDRESS:
log.Debug().Msg("DADR is remote station")
if *destinationAddress.AddrNet == *adapter.adapterNet {
log.Warn().Msg("path error (3)")
return nil
}
processLocally = *destinationAddress.AddrNet == *n.localAdapter.adapterNet && bytes.Compare(destinationAddress.AddrAddress, n.localAdapter.adapterAddr.AddrAddress) == 0
forwardMessage = !processLocally
case destinationAddress.AddrType == GLOBAL_BROADCAST_ADDRESS:
log.Debug().Msg("DADR is global broadcast")
processLocally = true
forwardMessage = true
default:
log.Warn().Stringer("addrType", destinationAddress.AddrType).Msg("invalid destination address type:")
return nil
}
log.Debug().Bool("processLocally", processLocally).Msg("processLocally")
log.Debug().Bool("forwardMessage", forwardMessage).Msg("forwardMessage")
// application or network layer message
if !npdu.GetControl().GetMessageTypeFieldPresent() {
log.Debug().Msg("application layer message")
// decode as a generic APDU
apdu := NewPDU(npdu.GetApdu())
log.Debug().Stringer("apdu", apdu).Msg("apdu")
// see if it needs to look routed
if len(n.adapters) > 1 && adapter != n.localAdapter {
// combine the source address
if !npdu.GetControl().GetSourceSpecified() {
remoteStationAddress, err := NewAddress(adapter.adapterNet, pdu.GetPDUSource().AddrAddress)
if err != nil {
return errors.Wrap(err, "error creating remote address")
}
apdu.pduSource = remoteStationAddress
} else {
apdu.pduSource = sourceAddress
}
if settings.RouteAware {
apdu.pduSource.AddrRoute = pdu.GetPDUSource()
}
// map the destination
if !npdu.GetControl().GetDestinationSpecified() {
apdu.pduDestination = n.localAdapter.adapterAddr
} else if destinationAddress.AddrType == GLOBAL_BROADCAST_ADDRESS {
apdu.pduDestination = NewGlobalBroadcast(nil)
} else if destinationAddress.AddrType == GLOBAL_BROADCAST_ADDRESS {
apdu.pduDestination = NewLocalBroadcast(nil)
} else {
apdu.pduDestination = n.localAdapter.adapterAddr
}
} else {
// combine the source address
if npdu.GetControl().GetSourceSpecified() {
apdu.pduSource = sourceAddress
if settings.RouteAware {
log.Debug().Msg("adding route")
apdu.pduSource = pdu.GetPDUSource()
}
} else {
apdu.pduSource = pdu.GetPDUSource()
}
// pass along global broadcast
if npdu.GetControl().GetDestinationSpecified() && destinationAddress.AddrType == GLOBAL_BROADCAST_ADDRESS {
apdu.pduDestination = NewGlobalBroadcast(nil)
} else {
apdu.pduDestination = pdu.GetPDUDestination()
}
}
log.Debug().Stringer("pduSource", apdu.pduSource).Msg("apdu.pduSource")
log.Debug().Stringer("pduDestination", apdu.pduDestination).Msg("apdu.pduDestination")
if err := n.Response(apdu); err != nil {
return errors.Wrap(err, "error passing response")
}
} else {
log.Debug().Msg("network layer message")
if processLocally {
log.Debug().Msg("processing NPDU locally")
// pass to the service element
// TODO: how to pass the adapter???
if err := n.SapRequest(pdu); err != nil {
return errors.Wrap(err, "error passing sap _request")
}
}
}
// might not need to forward this to other devices
if !forwardMessage {
log.Debug().Msg("no forwarding")
return nil
}
// make sure we're really a router
if len(n.adapters) == 1 {
log.Debug().Msg("not a router")
return nil
}
// make sure it hasn't looped
if npdu.GetHopCount() != nil && *npdu.GetHopCount() == 0 {
log.Debug().Msg("no more hops")
return nil
}
// build a new NPDU to send to other adapters
newpdu := NewPDUFromPDU(pdu)
// decrease the hop count
newNpduHopCount := *npdu.GetHopCount() - 1
// set the source address
var newSADR *Address
if !npdu.GetControl().GetSourceSpecified() {
var err error
newSADR, err = NewRemoteStation(adapter.adapterNet, sourceAddress.AddrAddress, nil)
if err != nil {
return errors.Wrap(err, "error creating remote station")
}
} else {
newSADR = destinationAddress
}
var newDADR *Address
// If this is a broadcast it goes everywhere
if destinationAddress.AddrType == GLOBAL_BROADCAST_ADDRESS {
log.Debug().Msg("global broadcasting")
newDADR = NewLocalBroadcast(nil)
newSADRLength := uint8(len(newSADR.AddrAddress))
newDADRLength := uint8(len(newDADR.AddrAddress))
newpdu.pduUserData = readWriteModel.NewNPDU(
npdu.GetProtocolVersionNumber(),
readWriteModel.NewNPDUControl(
false,
true,
true,
false,
readWriteModel.NPDUNetworkPriority_NORMAL_MESSAGE,
),
newDADR.AddrNet,
&newDADRLength,
newDADR.AddrAddress,
newSADR.AddrNet,
&newSADRLength,
newSADR.AddrAddress,
&newNpduHopCount,
nil,
npdu.GetApdu(),
0,
)
for _, xadapter := range n.adapters {
if xadapter != adapter {
if err := xadapter.ProcessNPDU(NewPDUFromPDU(newpdu)); err != nil {
log.Warn().Err(err).Msg("Error processing npdu")
}
}
}
return nil
}
if destinationAddress.AddrType == REMOTE_BROADCAST_ADDRESS || destinationAddress.AddrType == REMOTE_STATION_ADDRESS {
dnet := destinationAddress.AddrNet
log.Debug().Msg("remote station/broadcast")
// see if this a locally connected network
if xadapter, ok := n.adapters[dnet]; ok {
if xadapter == adapter {
log.Debug().Msg("path error (4)")
return nil
}
log.Debug().Stringer("adapter", adapter).Msg("found path via")
// if this was a remote broadcast, it's now a local one
if destinationAddress.AddrType == REMOTE_BROADCAST_ADDRESS {
newDADR = NewLocalBroadcast(nil)
} else {
var err error
newDADR, err = NewLocalStation(destinationAddress.AddrAddress, nil)
if err != nil {
return errors.Wrap(err, "error building local station")
}
}
// last leg in routing
newDADR = nil
// send the packet downstream
newSADRLength := uint8(len(newSADR.AddrAddress))
newpdu.pduUserData = readWriteModel.NewNPDU(
npdu.GetProtocolVersionNumber(),
readWriteModel.NewNPDUControl(
false,
false,
true,
false,
readWriteModel.NPDUNetworkPriority_NORMAL_MESSAGE,
),
nil,
nil,
nil,
newSADR.AddrNet,
&newSADRLength,
newSADR.AddrAddress,
&newNpduHopCount,
nil,
npdu.GetApdu(),
0,
)
return xadapter.ProcessNPDU(NewPDUFromPDU(newpdu))
}
// look for routing information from the network of one of our adapters to the destination network
var routerInfo *RouterInfo
var snetAdapter *NetworkAdapter
for snet, _snetAdapter := range n.adapters {
if _routerInfo := n.routerInfoCache.GetRouterInfo(snet, dnet); _routerInfo != nil {
routerInfo = _routerInfo
snetAdapter = _snetAdapter
break
}
}
// found a path
if routerInfo != nil {
log.Debug().Stringer("routerInfo", routerInfo).Msg("found path via routerInfo")
// the destination is the address of the router
pduDestination := routerInfo.address
// send the packet downstream
return snetAdapter.ProcessNPDU(NewPDUFromPDU(newpdu, WithPDUDestination(&pduDestination)))
}
log.Debug().Msg("No router info found")
// try to find a path to the network
xnpdu := readWriteModel.NewNLMWhoIsRouterToNetwork(dnet, 0)
pduDestination := NewLocalBroadcast(nil)
// send it to all of the connected adapters
for _, xadapter := range n.adapters {
// skip the horse it rode in on
if xadapter == adapter {
continue
}
// pass this along as if it came from the NSE
if err := n.SapIndicationWithAdapter(xadapter, NewPDU(xnpdu, WithPDUDestination(pduDestination))); err != nil {
return errors.Wrap(err, "error sending indication")
}
}
return nil
}
log.Debug().
Interface("destinationNetworkAddress", npdu.GetDestinationNetworkAddress()).
Interface("destinationAddress", npdu.GetDestinationAddress()).
Msg("bad DADR")
return nil
}
func (n *NetworkServiceAccessPoint) SapIndication(npdu _PDU) error {
panic("unused")
}
func (n *NetworkServiceAccessPoint) SapIndicationWithAdapter(adapter *NetworkAdapter, npdu _PDU) error {
log.Debug().Stringer("adapter", adapter).Stringer("npdu", npdu).Msg("SapIndication")
// encode it as a generic NPDU
// TODO: we don't need that as a npdu is a npdu
// tell the adapter to process the NPDU
return adapter.ProcessNPDU(npdu)
}
func (n *NetworkServiceAccessPoint) SapConfirmation(npdu _PDU) error {
panic("unused")
}
func (n *NetworkServiceAccessPoint) SapConfirmationWithAdapter(adapter *NetworkAdapter, npdu _PDU) error {
log.Debug().Stringer("adapter", adapter).Stringer("npdu", npdu).Msg("SapConfirmationWithAdapter")
// encode it as a generic NPDU
// TODO: we don't need that as a npdu is a npdu
return adapter.ProcessNPDU(npdu)
}
type NetworkServiceElement struct {
*ApplicationServiceElement
// TODO: implement me
}
func NewNetworkServiceElement(eid *int) (*NetworkServiceElement, error) {
n := &NetworkServiceElement{}
var err error
n.ApplicationServiceElement, err = NewApplicationServiceElement(eid, n)
if err != nil {
return nil, errors.Wrap(err, "error creating application service element")
}
Deferred(n.Startup)
return n, nil
}
func (n *NetworkServiceElement) Startup() error {
log.Debug().Msg("Startup")
// reference the service access point
sap := n.elementService.(*NetworkServiceAccessPoint) // TODO: hard cast but seems like adapters appears first in network service access point (so hard binding)
log.Debug().Stringer("sap", sap).Msg("sap")
// loop through all the adapters
// TODO: no adapters yet
return nil
}