forked from YutaroHayakawa/go-ra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_socket.go
134 lines (110 loc) · 2.49 KB
/
fake_socket.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of go-ra
package ra
import (
"context"
"fmt"
"net"
"net/netip"
"sync"
"sync/atomic"
"time"
"github.com/mdlayher/ndp"
)
type fakeSockRegistry struct {
reg map[string]*fakeSock
regLock sync.RWMutex
}
func newFakeSockRegistry() *fakeSockRegistry {
return &fakeSockRegistry{
reg: map[string]*fakeSock{},
}
}
func (r *fakeSockRegistry) newSock(iface string) (socket, error) {
r.regLock.Lock()
defer r.regLock.Unlock()
if _, ok := r.reg[iface]; ok {
return nil, fmt.Errorf("duplicate interface name")
}
fs := &fakeSock{
txMulticast: make(chan fakeRA, 128),
txLLUnicast: make(chan fakeRA, 128),
rx: make(chan fakeRS, 128),
}
r.reg[iface] = fs
return fs, nil
}
func (r *fakeSockRegistry) getSock(iface string) (*fakeSock, error) {
r.regLock.RLock()
defer r.regLock.RUnlock()
fs, ok := r.reg[iface]
if !ok {
return nil, fmt.Errorf("interface not found")
}
return fs, nil
}
// A fake socket
type fakeSock struct {
txMulticast chan fakeRA
txLLUnicast chan fakeRA
rx chan fakeRS
closed atomic.Bool
}
type fakeRA struct {
tstamp time.Time
msg *ndp.RouterAdvertisement
to netip.Addr
}
type fakeRS struct {
msg *ndp.RouterSolicitation
from netip.Addr
}
var _ socket = &fakeSock{}
func (s *fakeSock) txMulticastCh() <-chan fakeRA {
return s.txMulticast
}
func (s *fakeSock) txLLUnicastCh() <-chan fakeRA {
return s.txLLUnicast
}
func (s *fakeSock) rxCh() chan<- fakeRS {
return s.rx
}
func (s *fakeSock) hardwareAddr() net.HardwareAddr {
return net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
}
func (s *fakeSock) sendRA(_ context.Context, addr netip.Addr, msg *ndp.RouterAdvertisement) error {
ra := fakeRA{tstamp: time.Now(), msg: msg, to: addr}
if addr.IsMulticast() {
select {
case s.txMulticast <- ra:
return nil
default:
return fmt.Errorf("tx multicast channel is full")
}
} else if addr.IsLinkLocalUnicast() {
select {
case s.txLLUnicast <- ra:
return nil
default:
return fmt.Errorf("tx link-local unicast channel is full")
}
} else {
return fmt.Errorf("unsupported address type")
}
}
func (s *fakeSock) recvRS(ctx context.Context) (*ndp.RouterSolicitation, netip.Addr, error) {
select {
case <-ctx.Done():
return nil, netip.Addr{}, ctx.Err()
case rs := <-s.rx:
return rs.msg, rs.from, nil
}
}
func (s *fakeSock) close() {
close(s.txMulticast)
close(s.rx)
s.closed.Store(true)
}
func (s *fakeSock) isClosed() bool {
return s.closed.Load()
}