This introduces interfaces to represent the packet forwarding mechanism, and brings all the scattered UDP-related code together into a single implementation. This is not a pure refactoring, although much of the code in sleeve.go has been copied from pre-existing code, and the UDP protocol remains the same in spirit. The biggest change is the organization of the forwarder goroutines (there is now one instead of two, and that goroutine does a lot of auxiliary work as well as forwarding). Performance testing between VMs on my laptop suggests that packet throughput has not gone down, and may even have improved slightly.
99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
package router
|
|
|
|
import (
|
|
"net"
|
|
)
|
|
|
|
// Interface to overlay network packet handling
|
|
type Overlay interface {
|
|
// Start consuming forwarded packets.
|
|
StartConsumingPackets(*Peer, *Peers, OverlayConsumer) error
|
|
|
|
// Form a packet-forwarding connection.
|
|
MakeForwarder(ForwarderParams) (OverlayForwarder, error)
|
|
}
|
|
|
|
type ForwarderParams struct {
|
|
RemotePeer *Peer
|
|
|
|
// The local IP address to use for sending. Derived from the
|
|
// local address of the corresponding TCP socket, so may
|
|
// differ for different forwarders.
|
|
LocalIP net.IP
|
|
|
|
// The remote address to send to. nil if unknown, i.e. an
|
|
// incoming connection, in which case the Overlay needs to
|
|
// discover it (e.g. from incoming datagrams).
|
|
RemoteAddr *net.UDPAddr
|
|
|
|
// Unique identifier for this connection
|
|
ConnUID uint64
|
|
|
|
// Crypto bits. Nil if not encrypting
|
|
Crypto *OverlayCrypto
|
|
|
|
// Function to send a control message to the counterpart
|
|
// forwarder.
|
|
SendControlMessage func([]byte) error
|
|
}
|
|
|
|
// When a consumer is called, the decoder will already have been used
|
|
// to decode the frame.
|
|
type OverlayConsumer func(src *Peer, dst *Peer, frame []byte,
|
|
dec *EthernetDecoder)
|
|
|
|
// Crypto settings for a forwarder.
|
|
type OverlayCrypto struct {
|
|
Dec Decryptor
|
|
Enc Encryptor
|
|
EncDF Encryptor
|
|
}
|
|
|
|
// All of the machinery to forward packets to a particular peer
|
|
type OverlayForwarder interface {
|
|
// Register a callback for forwarder state changes.
|
|
// side-effect, calling this confirms that the connection is
|
|
// really wanted, and so the provider should activate it.
|
|
// However, Forward might be called before this is called
|
|
// (e.g. on another thread).
|
|
SetListener(OverlayForwarderListener)
|
|
|
|
// Forward a packet across the connection. The caller must
|
|
// supply an EthernetDecoder specific to this thread, which
|
|
// has already been used to decode the frame.
|
|
Forward(src *Peer, dest *Peer, frame []byte, dec *EthernetDecoder) error
|
|
|
|
Stop()
|
|
|
|
// Handle a message from the peer
|
|
ControlMessage([]byte)
|
|
}
|
|
|
|
type OverlayForwarderListener interface {
|
|
Established()
|
|
Error(error)
|
|
}
|
|
|
|
type NullOverlay struct{}
|
|
|
|
func (NullOverlay) StartConsumingPackets(*Peer, *Peers, OverlayConsumer) error {
|
|
return nil
|
|
}
|
|
|
|
func (NullOverlay) MakeForwarder(ForwarderParams) (OverlayForwarder, error) {
|
|
return NullOverlay{}, nil
|
|
}
|
|
|
|
func (NullOverlay) SetListener(OverlayForwarderListener) {
|
|
}
|
|
|
|
func (NullOverlay) Forward(*Peer, *Peer, []byte, *EthernetDecoder) error {
|
|
return nil
|
|
}
|
|
|
|
func (NullOverlay) Stop() {
|
|
}
|
|
|
|
func (NullOverlay) ControlMessage([]byte) {
|
|
}
|