mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-29 20:11:36 +00: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
@ -112,33 +112,10 @@ func (tun *TunAdapter) writer() error {
|
|||||||
|
|
||||||
func (tun *TunAdapter) reader() error {
|
func (tun *TunAdapter) reader() error {
|
||||||
recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
|
recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
|
||||||
for {
|
toWorker := make(chan []byte, 32)
|
||||||
// Wait for a packet to be delivered to us through the TUN/TAP adapter
|
defer close(toWorker)
|
||||||
n, err := tun.iface.Read(recvd)
|
go func() {
|
||||||
if err != nil {
|
for bs := range toWorker {
|
||||||
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
|
|
||||||
// If we detect an ICMP packet then hand it to the ICMPv6 module
|
// If we detect an ICMP packet then hand it to the ICMPv6 module
|
||||||
if bs[6] == 58 {
|
if bs[6] == 58 {
|
||||||
// Found an ICMPv6 packet - we need to make sure to give ICMPv6 the full
|
// 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 dstNodeIDMask *crypto.NodeID
|
||||||
var dstSnet address.Subnet
|
var dstSnet address.Subnet
|
||||||
var addrlen int
|
var addrlen int
|
||||||
|
n := len(bs)
|
||||||
// Check the IP protocol - if it doesn't match then we drop the packet and
|
// Check the IP protocol - if it doesn't match then we drop the packet and
|
||||||
// do nothing with it
|
// do nothing with it
|
||||||
if bs[0]&0xf0 == 0x60 {
|
if bs[0]&0xf0 == 0x60 {
|
||||||
@ -240,7 +218,7 @@ func (tun *TunAdapter) reader() error {
|
|||||||
panic("Given empty dstNodeID and dstNodeIDMask - this shouldn't happen")
|
panic("Given empty dstNodeID and dstNodeIDMask - this shouldn't happen")
|
||||||
}
|
}
|
||||||
// Dial to the remote node
|
// Dial to the remote node
|
||||||
packet := append(util.GetBytes(), bs[:n]...)
|
packet := bs
|
||||||
go func() {
|
go func() {
|
||||||
// FIXME just spitting out a goroutine to do this is kind of ugly and means we drop packets until the dial finishes
|
// 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()
|
tun.mutex.Lock()
|
||||||
@ -283,7 +261,7 @@ func (tun *TunAdapter) reader() error {
|
|||||||
}
|
}
|
||||||
// If we have a connection now, try writing to it
|
// If we have a connection now, try writing to it
|
||||||
if isIn && session != nil {
|
if isIn && session != nil {
|
||||||
packet := append(util.GetBytes(), bs[:n]...)
|
packet := bs
|
||||||
select {
|
select {
|
||||||
case session.send <- packet:
|
case session.send <- packet:
|
||||||
default:
|
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…
Reference in New Issue
Block a user