mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-10 06:20:26 +00:00
Remove commented out router function
This commit is contained in:
parent
de1005e4fa
commit
377f88512b
@ -138,181 +138,6 @@ func (r *router) mainLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Checks a packet's to/from address to make sure it's in the allowed range.
|
||||
// If a session to the destination exists, gets the session and passes the packet to it.
|
||||
// If no session exists, it triggers (or continues) a search.
|
||||
// If the session hasn't responded recently, it triggers a ping or search to keep things alive or deal with broken coords *relatively* quickly.
|
||||
// It also deals with oversized packets if there are MTU issues by calling into icmpv6.go to spoof PacketTooBig traffic, or DestinationUnreachable if the other side has their adapter disabled.
|
||||
func (r *router) sendPacket(bs []byte) {
|
||||
var sourceAddr address.Address
|
||||
var destAddr address.Address
|
||||
var destSnet address.Subnet
|
||||
var destPubKey *crypto.BoxPubKey
|
||||
var destNodeID *crypto.NodeID
|
||||
var addrlen int
|
||||
if bs[0]&0xf0 == 0x60 {
|
||||
// Check if we have a fully-sized header
|
||||
if len(bs) < 40 {
|
||||
panic("Tried to send a packet shorter than an IPv6 header...")
|
||||
}
|
||||
// IPv6 address
|
||||
addrlen = 16
|
||||
copy(sourceAddr[:addrlen], bs[8:])
|
||||
copy(destAddr[:addrlen], bs[24:])
|
||||
copy(destSnet[:addrlen/2], bs[24:])
|
||||
} else if bs[0]&0xf0 == 0x40 {
|
||||
// Check if we have a fully-sized header
|
||||
if len(bs) < 20 {
|
||||
panic("Tried to send a packet shorter than an IPv4 header...")
|
||||
}
|
||||
// IPv4 address
|
||||
addrlen = 4
|
||||
copy(sourceAddr[:addrlen], bs[12:])
|
||||
copy(destAddr[:addrlen], bs[16:])
|
||||
} else {
|
||||
// Unknown address length
|
||||
return
|
||||
}
|
||||
if !r.cryptokey.isValidSource(sourceAddr, addrlen) {
|
||||
// The packet had a source address that doesn't belong to us or our
|
||||
// configured crypto-key routing source subnets
|
||||
return
|
||||
}
|
||||
if !destAddr.IsValid() && !destSnet.IsValid() {
|
||||
// The addresses didn't match valid Yggdrasil node addresses so let's see
|
||||
// whether it matches a crypto-key routing range instead
|
||||
if key, err := r.cryptokey.getPublicKeyForAddress(destAddr, addrlen); err == nil {
|
||||
// A public key was found, get the node ID for the search
|
||||
destPubKey = &key
|
||||
destNodeID = crypto.GetNodeID(destPubKey)
|
||||
// Do a quick check to ensure that the node ID refers to a vaild Yggdrasil
|
||||
// address or subnet - this might be superfluous
|
||||
addr := *address.AddrForNodeID(destNodeID)
|
||||
copy(destAddr[:], addr[:])
|
||||
copy(destSnet[:], addr[:])
|
||||
if !destAddr.IsValid() && !destSnet.IsValid() {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// No public key was found in the CKR table so we've exhausted our options
|
||||
return
|
||||
}
|
||||
}
|
||||
searchCompleted := func(sinfo *sessionInfo, err error) {
|
||||
if err != nil {
|
||||
r.core.log.Debugln("DHT search failed:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
doSearch := func(packet []byte) {
|
||||
var nodeID, mask *crypto.NodeID
|
||||
switch {
|
||||
case destNodeID != nil:
|
||||
// We already know the full node ID, probably because it's from a CKR
|
||||
// route in which the public key is known ahead of time
|
||||
nodeID = destNodeID
|
||||
var m crypto.NodeID
|
||||
for i := range m {
|
||||
m[i] = 0xFF
|
||||
}
|
||||
mask = &m
|
||||
case destAddr.IsValid():
|
||||
// We don't know the full node ID - try and use the address to generate
|
||||
// a truncated node ID
|
||||
nodeID, mask = destAddr.GetNodeIDandMask()
|
||||
case destSnet.IsValid():
|
||||
// We don't know the full node ID - try and use the subnet to generate
|
||||
// a truncated node ID
|
||||
nodeID, mask = destSnet.GetNodeIDandMask()
|
||||
default:
|
||||
return
|
||||
}
|
||||
sinfo, isIn := r.core.searches.searches[*nodeID]
|
||||
if !isIn {
|
||||
sinfo = r.core.searches.newIterSearch(nodeID, mask, searchCompleted)
|
||||
}
|
||||
if packet != nil {
|
||||
sinfo.packet = packet
|
||||
}
|
||||
r.core.searches.continueSearch(sinfo)
|
||||
}
|
||||
var sinfo *sessionInfo
|
||||
var isIn bool
|
||||
if destAddr.IsValid() {
|
||||
sinfo, isIn = r.core.sessions.getByTheirAddr(&destAddr)
|
||||
}
|
||||
if destSnet.IsValid() {
|
||||
sinfo, isIn = r.core.sessions.getByTheirSubnet(&destSnet)
|
||||
}
|
||||
sTime := sinfo.time.Load().(time.Time)
|
||||
pingTime := sinfo.pingTime.Load().(time.Time)
|
||||
pingSend := sinfo.pingSend.Load().(time.Time)
|
||||
switch {
|
||||
case !isIn || !sinfo.init.Load().(bool):
|
||||
// No or unintiialized session, so we need to search first
|
||||
doSearch(bs)
|
||||
case time.Since(sTime) > 6*time.Second:
|
||||
if sTime.Before(pingTime) && time.Since(pingTime) > 6*time.Second {
|
||||
// We haven't heard from the dest in a while
|
||||
// We tried pinging but didn't get a response
|
||||
// They may have changed coords
|
||||
// Try searching to discover new coords
|
||||
// Note that search spam is throttled internally
|
||||
doSearch(nil)
|
||||
} else {
|
||||
// We haven't heard about the dest in a while
|
||||
now := time.Now()
|
||||
|
||||
if !sTime.Before(pingTime) {
|
||||
// Update pingTime to start the clock for searches (above)
|
||||
sinfo.pingTime.Store(now)
|
||||
}
|
||||
if time.Since(pingSend) > time.Second {
|
||||
// Send at most 1 ping per second
|
||||
sinfo.pingSend.Store(now)
|
||||
r.core.sessions.sendPingPong(sinfo, false)
|
||||
}
|
||||
}
|
||||
fallthrough // Also send the packet
|
||||
default:
|
||||
// If we know the public key ahead of time (i.e. a CKR route) then check
|
||||
// if the session perm pub key matches before we send the packet to it
|
||||
if destPubKey != nil {
|
||||
if !bytes.Equal((*destPubKey)[:], sinfo.theirPermPub[:]) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Drop packets if the session MTU is 0 - this means that one or other
|
||||
// side probably has their TUN adapter disabled
|
||||
if sinfo.getMTU() == 0 {
|
||||
// Don't continue - drop the packet
|
||||
return
|
||||
}
|
||||
// Generate an ICMPv6 Packet Too Big for packets larger than session MTU
|
||||
if len(bs) > int(sinfo.getMTU()) {
|
||||
// Get the size of the oversized payload, up to a max of 900 bytes
|
||||
window := 900
|
||||
if int(sinfo.getMTU()) < window {
|
||||
window = int(sinfo.getMTU())
|
||||
}
|
||||
|
||||
// Send the error back to the adapter
|
||||
r.reject <- RejectedPacket{
|
||||
Reason: PacketTooBig,
|
||||
Packet: bs[:window],
|
||||
Detail: int(sinfo.getMTU()),
|
||||
}
|
||||
|
||||
// Don't continue - drop the packet
|
||||
return
|
||||
}
|
||||
sinfo.send <- bs
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Checks incoming traffic type and passes it to the appropriate handler.
|
||||
func (r *router) handleIn(packet []byte) {
|
||||
pType, pTypeLen := wire_decode_uint64(packet)
|
||||
|
Loading…
Reference in New Issue
Block a user