mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	move router.recvPacket calls into the main router goroutine, to make the ckr checks threadsafe
This commit is contained in:
		
							parent
							
								
									ae4107a3b2
								
							
						
					
					
						commit
						5fa23b1e38
					
				
					 2 changed files with 18 additions and 8 deletions
				
			
		| 
						 | 
					@ -36,15 +36,22 @@ type router struct {
 | 
				
			||||||
	core      *Core
 | 
						core      *Core
 | 
				
			||||||
	addr      address
 | 
						addr      address
 | 
				
			||||||
	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"
 | 
				
			||||||
	recv      chan<- []byte // place where the tun pulls received packets from
 | 
						toRecv    chan router_recvPacket // packets to handle via recvPacket()
 | 
				
			||||||
	send      <-chan []byte // place where the tun puts outgoing packets
 | 
						recv      chan<- []byte          // place where the tun pulls received packets from
 | 
				
			||||||
	reset     chan struct{} // signal that coords changed (re-init sessions/dht)
 | 
						send      <-chan []byte          // place where the tun puts outgoing packets
 | 
				
			||||||
	admin     chan func()   // pass a lambda for the admin socket to query stuff
 | 
						reset     chan struct{}          // signal that coords changed (re-init sessions/dht)
 | 
				
			||||||
 | 
						admin     chan func()            // pass a lambda for the admin socket to query stuff
 | 
				
			||||||
	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:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue