-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdefinitions.go
More file actions
262 lines (232 loc) · 6.72 KB
/
Copy pathdefinitions.go
File metadata and controls
262 lines (232 loc) · 6.72 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
package internet
import (
"log/slog"
"math"
"net"
"slices"
"github.com/soypat/lneto"
"github.com/soypat/lneto/internal"
)
// node is a concrete StackNode as stored in Stacks. Methods are devirtualized for performance benefits, especially on TinyGo.
type node struct {
// currConnID stores the stack node *connID value on registration.
currConnID uint64
// connID is StackNode.ConnectionID() return value.
connID *uint64
// cbnode has different definitions in tinygo and normal Go compiled programs
// for performance and heap control reasons.
callbacks cbnode
// remoteAddr will be set on active(outbound) port connections
// that require an ARP to set the remoteAddr beforehand.
remoteAddr []byte
proto uint16 // StackNode.Protocol()
lport uint16 // StackNode.LocalPort()
}
type handlers struct {
nodes []node
// encapsIdx stores the index of next node to check for encapsulation.
encapsIdx int
context string
logger
}
func (h *handlers) reset(context string, maxNodes int) {
h.nodes = slices.Grow(h.nodes[:0], maxNodes)
h.context = context
}
func (h *handlers) registerByProto(n node) error {
err := h.prepAdd()
if err != nil {
return err
}
if h.nodeByProto(n.proto) != nil {
return lneto.ErrAlreadyRegistered
}
h.nodes = append(h.nodes, n)
return nil
}
func (h *handlers) registerByPortProto(n node) error {
err := h.prepAdd()
if err != nil {
return err
}
if h.nodeByPortProto(n.lport, n.proto) != nil {
return lneto.ErrAlreadyRegistered
}
h.nodes = append(h.nodes, n)
return nil
}
func (h *handlers) prepAdd() error {
if h.full() {
h.compact()
if h.full() {
return lneto.ErrExhausted
}
}
return nil
}
func (h *handlers) full() bool { return cap(h.nodes) == len(h.nodes) }
func (h *handlers) compact() {
nilOff := 0
for i := 0; i < len(h.nodes); i++ {
if !h.nodes[i].IsInvalid() {
h.nodes[nilOff] = h.nodes[i]
nilOff++
}
}
h.nodes = h.nodes[:nilOff]
}
func (h *handlers) tryHandleError(node *node, err error) (discardedGracefully bool) {
if err != nil && (err == net.ErrClosed || node.IsInvalid()) {
node.destroy()
discardedGracefully = true
}
return discardedGracefully
}
func (h *handlers) nodeByProto(proto uint16) *node {
for i := range h.nodes {
node := &h.nodes[i]
if node.proto == proto && !node.IsInvalid() {
return node
}
}
return nil
}
func (h *handlers) nodeByPort(port uint16) *node {
for i := range h.nodes {
node := &h.nodes[i]
if node.lport == port && !node.IsInvalid() {
return node
}
}
return nil
}
func (h *handlers) nodeByPortProto(port uint16, protocol uint16) *node {
for i := range h.nodes {
node := &h.nodes[i]
if node.lport == port && node.proto == protocol && !node.IsInvalid() {
return node
}
}
return nil
}
func (h *handlers) demuxByProto(buf []byte, offset int, proto uint16) (*node, error) {
node := h.nodeByProto(proto)
if node == nil {
return nil, lneto.ErrPacketDrop
}
err := node.callbacks.Demux(buf, offset)
if h.tryHandleError(node, err) {
err = nil
}
return node, err
}
func (h *handlers) demuxByPort(buf []byte, offset int, port uint16) (*node, error) {
node := h.nodeByPort(port)
if node == nil {
return nil, lneto.ErrPacketDrop
}
err := node.callbacks.Demux(buf, offset)
if h.tryHandleError(node, err) {
err = nil
node = nil // Node is destroyed in tryHandleError and invalidated.
}
return node, err
}
func (h *handlers) encapsulateNode(node *node, buf []byte, offsetIP, offsetThisFrame int) (n int, err error) {
if node.IsInvalid() {
return 0, nil
}
n, err = node.callbacks.Encapsulate(buf, offsetIP, offsetThisFrame)
if h.tryHandleError(node, err) {
err = nil // CLOSE error handled gracefully by deleting node.
node = nil // Node is destroyed in tryHandleError and invalidated.
}
// TODO(soypat): We have fuzz tests in place, maybe we can start returning the error up the chain to catch invalid settings at application level so that users don't have to have logs in place to understand invalid config/buffer size.
// Encapsulate should only fail with error on programmer errors.
if n > 0 {
return n, err
} else if err != nil {
// Make sure not to hang on one handler that keeps returning an error.
h.error("handlers:encapsulate", slog.String("func", "encapsulateAny"), slog.String("ctx", h.context), slog.String("err", err.Error()))
}
return 0, nil
}
// encapsulateAny finds a node suitable to write and encapsulates the package.
// If no data is sent it returns the last error encountered.
func (h *handlers) encapsulateAny(buf []byte, offsetIP, offsetThisFrame int) (hn *node, n int, err error) {
// Round robin approach to encapsulation.
// TODO(soypat): benchmark impact of round robin. Consider removing fields from handlers to make it more lean and potentially get perf improvements that way.
i := h.encapsIdx
for range h.nodes {
hn := &h.nodes[i]
n, err = h.encapsulateNode(hn, buf, offsetIP, offsetThisFrame)
i = incLim(i, len(h.nodes))
if n > 0 || err != nil {
h.encapsIdx = i
return hn, n, err
}
}
return nil, 0, err // Return last written error.
}
var (
_ = net.ErrClosed
)
func (node *node) IsInvalid() bool {
return node.callbacks.IsZeroed() || (node.connID != nil && node.currConnID != *node.connID)
}
func checkNodeErr(node *node, err error) (discard bool) {
return node.IsInvalid() || (err != nil && err == net.ErrClosed)
}
func nodeFromStackNode(s lneto.StackNode, port uint16, protocol uint64, remoteAddr []byte) node {
if protocol > math.MaxUint16 {
panic(">16bit protocol number unsupported")
}
var currConnID uint64
connIDPtr := s.ConnectionID()
if connIDPtr != nil {
currConnID = *connIDPtr
}
return node{
currConnID: currConnID,
connID: connIDPtr,
callbacks: makecbnode(s),
proto: uint16(protocol),
lport: port,
remoteAddr: remoteAddr, // SHARED MEMORY- used to signal.
}
}
// destroy removes all references to underlying StackNode. Allows garbage collection of node if possible.
func (n *node) destroy() {
*n = node{}
}
func incLim(v, max int) int {
v++
if v == max {
v = 0
}
return v
}
type logger struct {
log *slog.Logger
}
func (l logger) error(msg string, attrs ...slog.Attr) {
internal.LogAttrs(l.log, slog.LevelError, msg, attrs...)
}
func (l logger) info(msg string, attrs ...slog.Attr) {
internal.LogAttrs(l.log, slog.LevelInfo, msg, attrs...)
}
func (l logger) warn(msg string, attrs ...slog.Attr) {
internal.LogAttrs(l.log, slog.LevelWarn, msg, attrs...)
}
func (l logger) debug(msg string, attrs ...slog.Attr) {
internal.LogAttrs(l.log, slog.LevelDebug, msg, attrs...)
}
func (l logger) trace(msg string, attrs ...slog.Attr) {
internal.LogAttrs(l.log, internal.LevelTrace, msg, attrs...)
}
const enableAllocLog = internal.HeapAllocDebugging
func debugLog(msg string) {
if enableAllocLog {
internal.LogAllocs(msg)
}
}