5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 09:31:38 +00:00

move router.recvPacket calls into the main router goroutine, to make the ckr checks threadsafe

This commit is contained in:
Arceliar 2018-11-20 22:04:18 -06:00
parent ae4107a3b2
commit 5fa23b1e38
2 changed files with 18 additions and 8 deletions

View File

@ -38,6 +38,7 @@ type router struct {
subnet subnet subnet subnet
in <-chan []byte // packets we received from the network, link to peer's "out" in <-chan []byte // packets we received from the network, link to peer's "out"
out func([]byte) // packets we're sending to the network, link to peer's "in" out func([]byte) // packets we're sending to the network, link to peer's "in"
toRecv chan router_recvPacket // packets to handle via recvPacket()
recv chan<- []byte // place where the tun pulls received packets from recv chan<- []byte // place where the tun pulls received packets from
send <-chan []byte // place where the tun puts outgoing packets send <-chan []byte // place where the tun puts outgoing packets
reset chan struct{} // signal that coords changed (re-init sessions/dht) reset chan struct{} // signal that coords changed (re-init sessions/dht)
@ -45,6 +46,12 @@ type router struct {
cryptokey cryptokey cryptokey cryptokey
} }
// Packet and session info, used to check that the packet matches a valid IP range or CKR prefix before sending to the tun.
type router_recvPacket struct {
bs []byte
sinfo *sessionInfo
}
// Initializes the router struct, which includes setting up channels to/from the tun/tap. // Initializes the router struct, which includes setting up channels to/from the tun/tap.
func (r *router) init(core *Core) { func (r *router) init(core *Core) {
r.core = core r.core = core
@ -63,6 +70,7 @@ func (r *router) init(core *Core) {
} }
r.in = in r.in = in
r.out = func(packet []byte) { p.handlePacket(packet) } // The caller is responsible for go-ing if it needs to not block r.out = func(packet []byte) { p.handlePacket(packet) } // The caller is responsible for go-ing if it needs to not block
r.toRecv = make(chan router_recvPacket, 32)
recv := make(chan []byte, 32) recv := make(chan []byte, 32)
send := make(chan []byte, 32) send := make(chan []byte, 32)
r.recv = recv r.recv = recv
@ -70,7 +78,7 @@ func (r *router) init(core *Core) {
r.core.tun.recv = recv r.core.tun.recv = recv
r.core.tun.send = send r.core.tun.send = send
r.reset = make(chan struct{}, 1) r.reset = make(chan struct{}, 1)
r.admin = make(chan func()) r.admin = make(chan func(), 32)
r.cryptokey.init(r.core) r.cryptokey.init(r.core)
// go r.mainLoop() // go r.mainLoop()
} }
@ -91,6 +99,8 @@ func (r *router) mainLoop() {
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
case rp := <-r.toRecv:
r.recvPacket(rp.bs, rp.sinfo)
case p := <-r.in: case p := <-r.in:
r.handleIn(p) r.handleIn(p)
case p := <-r.send: case p := <-r.send:

View File

@ -589,5 +589,5 @@ func (sinfo *sessionInfo) doRecv(p *wire_trafficPacket) {
sinfo.updateNonce(&p.Nonce) sinfo.updateNonce(&p.Nonce)
sinfo.time = time.Now() sinfo.time = time.Now()
sinfo.bytesRecvd += uint64(len(bs)) sinfo.bytesRecvd += uint64(len(bs))
sinfo.core.router.recvPacket(bs, sinfo) sinfo.core.router.toRecv <- router_recvPacket{bs, sinfo}
} }