make nodes wield unique UUIDs, but tests are failing now
This commit is contained in:
parent
15897003a0
commit
a28a84eafb
1 changed files with 26 additions and 41 deletions
|
|
@ -21,7 +21,7 @@ type LuxRouter struct {
|
||||||
thisKey crypto.LuxKey
|
thisKey crypto.LuxKey
|
||||||
keyStore crypto.LuxKeyStore
|
keyStore crypto.LuxKeyStore
|
||||||
|
|
||||||
routes []LuxRoute
|
routes map[proto.LuxID]*LuxRoute
|
||||||
|
|
||||||
channelLock sync.RWMutex
|
channelLock sync.RWMutex
|
||||||
outbound []LuxChannel
|
outbound []LuxChannel
|
||||||
|
|
@ -34,7 +34,7 @@ func NewLuxRouter(key crypto.LuxKey, ks crypto.LuxKeyStore) LuxRouter {
|
||||||
return LuxRouter{
|
return LuxRouter{
|
||||||
thisKey: key,
|
thisKey: key,
|
||||||
keyStore: ks,
|
keyStore: ks,
|
||||||
routes: make([]LuxRoute, 0),
|
routes: make(map[proto.LuxID]*LuxRoute, 0),
|
||||||
outbound: make([]LuxChannel, 0),
|
outbound: make([]LuxChannel, 0),
|
||||||
inbound: make([]LuxChannel, 0),
|
inbound: make([]LuxChannel, 0),
|
||||||
dgramChan: make(chan LuxDatagram),
|
dgramChan: make(chan LuxDatagram),
|
||||||
|
|
@ -80,12 +80,12 @@ func (r *LuxRouter) CreateOutboundRoute(id proto.LuxID, chType LuxChannelType, u
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
r.routes = append(r.routes, LuxRoute{
|
r.routes[key.Id] = &LuxRoute{
|
||||||
Key: key,
|
Key: key,
|
||||||
Destination: channel.Address,
|
Destination: channel.Address,
|
||||||
Associated: r.addOutboundChannel(channel),
|
Associated: r.addOutboundChannel(channel),
|
||||||
Nonces: NewLuxNonceList(),
|
Nonces: NewLuxNonceList(),
|
||||||
})
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -95,12 +95,12 @@ func (r *LuxRouter) CreateInboundChannel(chType LuxChannelType, udpAddr string)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
r.routes = append(r.routes, LuxRoute{
|
r.routes[r.thisKey.Id] = &LuxRoute{
|
||||||
Key: r.thisKey,
|
Key: r.thisKey,
|
||||||
Destination: channel.Address,
|
Destination: channel.Address,
|
||||||
Associated: r.addInboundChannel(channel),
|
Associated: r.addInboundChannel(channel),
|
||||||
Nonces: NewLuxNonceList(),
|
Nonces: NewLuxNonceList(),
|
||||||
})
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -164,7 +164,7 @@ func (r *LuxRouter) Stop() {
|
||||||
r.outbound = r.outbound[0:]
|
r.outbound = r.outbound[0:]
|
||||||
r.channelLock.Unlock()
|
r.channelLock.Unlock()
|
||||||
|
|
||||||
r.routes = r.routes[0:]
|
r.routes = make(map[proto.LuxID]*LuxRoute)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LuxRouter) RecvDgram() LuxDatagram {
|
func (r *LuxRouter) RecvDgram() LuxDatagram {
|
||||||
|
|
@ -175,42 +175,27 @@ func udpAddrEqual(addr *net.UDPAddr, other *net.UDPAddr) bool {
|
||||||
return addr.IP.Equal(other.IP) && addr.Port == other.Port
|
return addr.IP.Equal(other.IP) && addr.Port == other.Port
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LuxRouter) getRouteIndex(udpAddr *net.UDPAddr) (LuxRoute, int) {
|
func (r *LuxRouter) GetRoutes() map[proto.LuxID]*LuxRoute {
|
||||||
for idx, route := range r.routes {
|
|
||||||
if udpAddrEqual(route.Destination, udpAddr) {
|
|
||||||
return route, idx
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return LuxRoute{}, -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *LuxRouter) getRouteIndexByID(id proto.LuxID) (LuxRoute, int) {
|
|
||||||
for idx, route := range r.routes {
|
|
||||||
if bytes.Equal(route.Key.Id.UUID[:], id.UUID[:]) {
|
|
||||||
return route, idx
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return LuxRoute{}, -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *LuxRouter) GetRoutes() []LuxRoute {
|
|
||||||
return r.routes
|
return r.routes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LuxRouter) GetRoute(udpAddr *net.UDPAddr) (LuxRoute, bool) {
|
func (r *LuxRouter) GetRoute(udpAddr *net.UDPAddr) (*LuxRoute, bool) {
|
||||||
route, idx := r.getRouteIndex(udpAddr)
|
for _, route := range r.routes {
|
||||||
return route, idx != -1
|
if udpAddrEqual(route.Destination, udpAddr) {
|
||||||
|
return route, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LuxRouter) DeleteRoute(route *LuxRoute) {
|
func (r *LuxRouter) DeleteRoute(route *LuxRoute) {
|
||||||
_, idx := r.getRouteIndex(route.Destination)
|
if _, ok := r.routes[route.Key.Id]; !ok {
|
||||||
if idx == -1 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.CloseChannel(route.Associated, false)
|
r.CloseChannel(route.Associated, false)
|
||||||
r.routes = append(r.routes[idx:], r.routes[:idx+1]...)
|
delete(r.routes, route.Key.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LuxRouter) Recv() (LuxPacket, error) {
|
func (r *LuxRouter) Recv() (LuxPacket, error) {
|
||||||
|
|
@ -235,7 +220,7 @@ func (r *LuxRouter) Recv() (LuxPacket, error) {
|
||||||
// check if LuxID matches
|
// check if LuxID matches
|
||||||
if !bytes.Equal(packet.Target.UUID[:], route.Key.Id.UUID[:]) {
|
if !bytes.Equal(packet.Target.UUID[:], route.Key.Id.UUID[:]) {
|
||||||
// not matches.. we discard route and throw away packet
|
// not matches.. we discard route and throw away packet
|
||||||
r.DeleteRoute(&route)
|
r.DeleteRoute(route)
|
||||||
return packet, errors.New("bogus packet from established route")
|
return packet, errors.New("bogus packet from established route")
|
||||||
/* NOTE:
|
/* NOTE:
|
||||||
* there may be rare situation where multiple clients behind NAT/CGNAT
|
* there may be rare situation where multiple clients behind NAT/CGNAT
|
||||||
|
|
@ -258,21 +243,21 @@ func (r *LuxRouter) Recv() (LuxPacket, error) {
|
||||||
|
|
||||||
if bytes.Equal(packet.Target.UUID[:], key.Id.UUID[:]) {
|
if bytes.Equal(packet.Target.UUID[:], key.Id.UUID[:]) {
|
||||||
// key UUID and decrypted UUID matching - create OR update route and return packet
|
// key UUID and decrypted UUID matching - create OR update route and return packet
|
||||||
|
var ok bool
|
||||||
var route *LuxRoute
|
var route *LuxRoute
|
||||||
if _, idx := r.getRouteIndexByID(packet.Target); idx != -1 {
|
if route, ok = r.routes[packet.Target]; ok {
|
||||||
route = &r.routes[idx]
|
|
||||||
route.Destination = dgram.Target
|
route.Destination = dgram.Target
|
||||||
route.Associated = dgram.Channel
|
route.Associated = dgram.Channel
|
||||||
// since packet arrived from different transport, we flush nonces
|
// since packet arrived from different transport, we flush nonces
|
||||||
route.Nonces.Flush()
|
route.Nonces.Flush()
|
||||||
} else {
|
} else {
|
||||||
r.routes = append(r.routes, LuxRoute{
|
r.routes[key.Id] = &LuxRoute{
|
||||||
Key: key,
|
Key: key,
|
||||||
Destination: dgram.Target,
|
Destination: dgram.Target,
|
||||||
Associated: dgram.Channel,
|
Associated: dgram.Channel,
|
||||||
Nonces: NewLuxNonceList(),
|
Nonces: NewLuxNonceList(),
|
||||||
})
|
}
|
||||||
route = &r.routes[len(r.routes)-1]
|
route = r.routes[key.Id]
|
||||||
}
|
}
|
||||||
|
|
||||||
// rotate nonce
|
// rotate nonce
|
||||||
|
|
@ -291,8 +276,8 @@ func (r *LuxRouter) Recv() (LuxPacket, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LuxRouter) Send(packet LuxPacket) error {
|
func (r *LuxRouter) Send(packet LuxPacket) error {
|
||||||
route, idx := r.getRouteIndexByID(packet.Target)
|
route, ok := r.routes[packet.Target]
|
||||||
if idx == -1 {
|
if !ok {
|
||||||
return errors.New("no route to peer")
|
return errors.New("no route to peer")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue