-
Notifications
You must be signed in to change notification settings - Fork 107
/
overlay.go
127 lines (98 loc) · 3.91 KB
/
overlay.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
package mesh
import (
"net"
)
// Overlay yields OverlayConnections.
type Overlay interface {
// Enhance a features map with overlay-related features.
AddFeaturesTo(map[string]string)
// Prepare on overlay connection. The connection should remain
// passive until it has been Confirm()ed.
PrepareConnection(OverlayConnectionParams) (OverlayConnection, error)
// Obtain diagnostic information specific to the overlay.
Diagnostics() interface{}
// Stop the overlay.
Stop()
}
// OverlayConnectionParams are used to set up overlay connections.
type OverlayConnectionParams struct {
RemotePeer *Peer
// The local address of the corresponding TCP connection. Used to
// derive the local IP address for sending. May differ for
// different overlay connections.
LocalAddr *net.TCPAddr
// The remote address of the corresponding TCP connection. Used to
// determine the address to send to, but only if the TCP
// connection is outbound. Otherwise the Overlay needs to discover
// it (e.g. from incoming datagrams).
RemoteAddr *net.TCPAddr
// Is the corresponding TCP connection outbound?
Outbound bool
// Unique identifier for this connection
ConnUID uint64
// Session key, if connection is encrypted; nil otherwise.
//
// NB: overlay connections must take care not to use nonces which
// may collide with those of the main connection. These nonces are
// 192 bits, with the top most bit unspecified, the next bit set
// to 1, followed by 126 zero bits, and a message sequence number
// in the lowest 64 bits.
SessionKey *[32]byte
// Function to send a control message to the counterpart
// overlay connection.
SendControlMessage func(tag byte, msg []byte) error
// Features passed at connection initiation
Features map[string]string
}
// OverlayConnection describes all of the machinery to manage overlay
// connectivity to a particular peer.
type OverlayConnection interface {
// Confirm that the connection is really wanted, and so the
// Overlay should begin heartbeats etc. to verify the operation of
// the overlay connection.
Confirm()
// EstablishedChannel returns a channel that will be closed when the
// overlay connection is established, i.e. its operation has been
// confirmed.
EstablishedChannel() <-chan struct{}
// ErrorChannel returns a channel that forwards errors from the overlay
// connection. The overlay connection is not expected to be operational
// after the first error, so the channel only needs to buffer a single
// error.
ErrorChannel() <-chan error
// Stop terminates the connection.
Stop()
// ControlMessage handles a message from the remote peer. 'tag' exists for
// compatibility, and should always be ProtocolOverlayControlMessage for
// non-sleeve overlays.
ControlMessage(tag byte, msg []byte)
// Attrs returns the user-facing overlay name plus any other
// data that users may wish to check or monitor
Attrs() map[string]interface{}
}
// NullOverlay implements Overlay and OverlayConnection with no-ops.
type NullOverlay struct{}
// AddFeaturesTo implements Overlay.
func (NullOverlay) AddFeaturesTo(map[string]string) {}
// PrepareConnection implements Overlay.
func (NullOverlay) PrepareConnection(OverlayConnectionParams) (OverlayConnection, error) {
return NullOverlay{}, nil
}
// Diagnostics implements Overlay.
func (NullOverlay) Diagnostics() interface{} { return nil }
// Confirm implements OverlayConnection.
func (NullOverlay) Confirm() {}
// EstablishedChannel implements OverlayConnection.
func (NullOverlay) EstablishedChannel() <-chan struct{} {
c := make(chan struct{})
close(c)
return c
}
// ErrorChannel implements OverlayConnection.
func (NullOverlay) ErrorChannel() <-chan error { return nil }
// Stop implements OverlayConnection.
func (NullOverlay) Stop() {}
// ControlMessage implements OverlayConnection.
func (NullOverlay) ControlMessage(byte, []byte) {}
// Attrs implements OverlayConnection.
func (NullOverlay) Attrs() map[string]interface{} { return nil }