87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package node
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
"net/url"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
bufferSize = 1536
|
|
if_mtu = 1200
|
|
if_queue_len = 2048
|
|
controlCipherOverhead = 16
|
|
dataCipherOverhead = 16
|
|
signOverhead = 64
|
|
)
|
|
|
|
var (
|
|
multicastIP = netip.AddrFrom4([4]byte{224, 0, 0, 157})
|
|
multicastAddr = net.UDPAddrFromAddrPort(netip.AddrPortFrom(multicastIP, 4560))
|
|
)
|
|
|
|
type peerRoute struct {
|
|
IP byte
|
|
Up bool // True if data can be sent on the route.
|
|
Relay bool // True if the peer is a relay.
|
|
Direct bool // True if this is a direct connection.
|
|
PubSignKey []byte
|
|
ControlCipher *controlCipher
|
|
DataCipher *dataCipher
|
|
RemoteAddr netip.AddrPort // Remote address if directly connected.
|
|
}
|
|
|
|
var (
|
|
hubURL *url.URL
|
|
apiKey string
|
|
|
|
// Configuration for this peer.
|
|
netName string
|
|
localIP byte
|
|
localPub bool
|
|
localAddr netip.AddrPort
|
|
privKey []byte
|
|
privSignKey []byte
|
|
|
|
// Shared interface for writing.
|
|
_iface *ifWriter
|
|
|
|
// Shared connection for writing.
|
|
_conn *connWriter
|
|
|
|
// Counters for sending to each peer.
|
|
sendCounters [256]uint64 = func() (out [256]uint64) {
|
|
for i := range out {
|
|
out[i] = uint64(time.Now().Unix()<<30 + 1)
|
|
}
|
|
return
|
|
}()
|
|
|
|
// Duplicate checkers for incoming packets.
|
|
dupChecks [256]*dupCheck = func() (out [256]*dupCheck) {
|
|
for i := range out {
|
|
out[i] = newDupCheck(0)
|
|
}
|
|
return
|
|
}()
|
|
|
|
// Messages for the supervisor.
|
|
messages = make(chan any, 1024)
|
|
|
|
// Global routing table.
|
|
routingTable [256]*atomic.Pointer[peerRoute] = func() (out [256]*atomic.Pointer[peerRoute]) {
|
|
for i := range out {
|
|
out[i] = &atomic.Pointer[peerRoute]{}
|
|
out[i].Store(&peerRoute{})
|
|
}
|
|
return
|
|
}()
|
|
|
|
// Managed by the relayManager.
|
|
relayIP = &atomic.Pointer[byte]{}
|
|
|
|
publicAddrs = newPubAddrStore()
|
|
)
|