mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	split up some of the tun reader logic into a separate worker, so the main loop can be mostly just syscalls
This commit is contained in:
		
							parent
							
								
									b66bea813b
								
							
						
					
					
						commit
						38e1503b28
					
				
					 1 changed files with 160 additions and 153 deletions
				
			
		| 
						 | 
				
			
			@ -112,33 +112,10 @@ func (tun *TunAdapter) writer() error {
 | 
			
		|||
 | 
			
		||||
func (tun *TunAdapter) reader() error {
 | 
			
		||||
	recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
 | 
			
		||||
	for {
 | 
			
		||||
		// Wait for a packet to be delivered to us through the TUN/TAP adapter
 | 
			
		||||
		n, err := tun.iface.Read(recvd)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			if !tun.isOpen {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
		if n == 0 {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		// If it's a TAP adapter, update the buffer slice so that we no longer
 | 
			
		||||
		// include the ethernet headers
 | 
			
		||||
		offset := 0
 | 
			
		||||
		if tun.iface.IsTAP() {
 | 
			
		||||
			// Set our offset to beyond the ethernet headers
 | 
			
		||||
			offset = tun_ETHER_HEADER_LENGTH
 | 
			
		||||
			// Check first of all that we can go beyond the ethernet headers
 | 
			
		||||
			if len(recvd) <= offset {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// Offset the buffer from now on so that we can ignore ethernet frames if
 | 
			
		||||
		// they are present
 | 
			
		||||
		bs := recvd[offset : offset+n]
 | 
			
		||||
		n -= offset
 | 
			
		||||
	toWorker := make(chan []byte, 32)
 | 
			
		||||
	defer close(toWorker)
 | 
			
		||||
	go func() {
 | 
			
		||||
		for bs := range toWorker {
 | 
			
		||||
			// If we detect an ICMP packet then hand it to the ICMPv6 module
 | 
			
		||||
			if bs[6] == 58 {
 | 
			
		||||
				// Found an ICMPv6 packet - we need to make sure to give ICMPv6 the full
 | 
			
		||||
| 
						 | 
				
			
			@ -159,6 +136,7 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
			var dstNodeIDMask *crypto.NodeID
 | 
			
		||||
			var dstSnet address.Subnet
 | 
			
		||||
			var addrlen int
 | 
			
		||||
			n := len(bs)
 | 
			
		||||
			// Check the IP protocol - if it doesn't match then we drop the packet and
 | 
			
		||||
			// do nothing with it
 | 
			
		||||
			if bs[0]&0xf0 == 0x60 {
 | 
			
		||||
| 
						 | 
				
			
			@ -240,7 +218,7 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
					panic("Given empty dstNodeID and dstNodeIDMask - this shouldn't happen")
 | 
			
		||||
				}
 | 
			
		||||
				// Dial to the remote node
 | 
			
		||||
			packet := append(util.GetBytes(), bs[:n]...)
 | 
			
		||||
				packet := bs
 | 
			
		||||
				go func() {
 | 
			
		||||
					// FIXME just spitting out a goroutine to do this is kind of ugly and means we drop packets until the dial finishes
 | 
			
		||||
					tun.mutex.Lock()
 | 
			
		||||
| 
						 | 
				
			
			@ -283,7 +261,7 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
			}
 | 
			
		||||
			// If we have a connection now, try writing to it
 | 
			
		||||
			if isIn && session != nil {
 | 
			
		||||
			packet := append(util.GetBytes(), bs[:n]...)
 | 
			
		||||
				packet := bs
 | 
			
		||||
				select {
 | 
			
		||||
				case session.send <- packet:
 | 
			
		||||
				default:
 | 
			
		||||
| 
						 | 
				
			
			@ -291,4 +269,33 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
	for {
 | 
			
		||||
		// Wait for a packet to be delivered to us through the TUN/TAP adapter
 | 
			
		||||
		n, err := tun.iface.Read(recvd)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			if !tun.isOpen {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
		if n == 0 {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		// If it's a TAP adapter, update the buffer slice so that we no longer
 | 
			
		||||
		// include the ethernet headers
 | 
			
		||||
		offset := 0
 | 
			
		||||
		if tun.iface.IsTAP() {
 | 
			
		||||
			// Set our offset to beyond the ethernet headers
 | 
			
		||||
			offset = tun_ETHER_HEADER_LENGTH
 | 
			
		||||
			// Check first of all that we can go beyond the ethernet headers
 | 
			
		||||
			if len(recvd) <= offset {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// Offset the buffer from now on so that we can ignore ethernet frames if
 | 
			
		||||
		// they are present
 | 
			
		||||
		bs := append(util.GetBytes(), recvd[offset:offset+n]...)
 | 
			
		||||
		toWorker <- bs
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue