5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-23 13:25:39 +00:00

move some logic from TunAdapter.reader into a new function, TunAdapter.readerPacketHandler

This commit is contained in:
Arceliar 2019-07-28 23:33:04 -05:00
parent 38e1503b28
commit 406e143f7f

View File

@ -110,12 +110,24 @@ func (tun *TunAdapter) writer() error {
}
}
func (tun *TunAdapter) reader() error {
recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
toWorker := make(chan []byte, 32)
defer close(toWorker)
go func() {
for bs := range toWorker {
// Run in a separate goroutine by the reader
// Does all of the per-packet ICMP checks, passes packets to the right Conn worker
func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
for recvd := range ch {
// 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:]
// 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
@ -127,6 +139,8 @@ func (tun *TunAdapter) reader() error {
continue
}
}
// Shift forward to avoid leaking bytes off the front of the slide when we eventually store it
bs = append(recvd[:0], bs...)
// From the IP header, work out what our source and destination addresses
// and node IDs are. We will need these in order to work out where to send
// the packet
@ -218,12 +232,11 @@ func (tun *TunAdapter) reader() error {
panic("Given empty dstNodeID and dstNodeIDMask - this shouldn't happen")
}
// Dial to the remote node
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()
_, known := tun.dials[*dstNodeID]
tun.dials[*dstNodeID] = append(tun.dials[*dstNodeID], packet)
tun.dials[*dstNodeID] = append(tun.dials[*dstNodeID], bs)
for len(tun.dials[*dstNodeID]) > 32 {
util.PutBytes(tun.dials[*dstNodeID][0])
tun.dials[*dstNodeID] = tun.dials[*dstNodeID][1:]
@ -261,15 +274,20 @@ func (tun *TunAdapter) reader() error {
}
// If we have a connection now, try writing to it
if isIn && session != nil {
packet := bs
select {
case session.send <- packet:
case session.send <- bs:
default:
util.PutBytes(packet)
util.PutBytes(bs)
}
}
}
}()
}
func (tun *TunAdapter) reader() error {
recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
toWorker := make(chan []byte, 32)
defer close(toWorker)
go tun.readerPacketHandler(toWorker)
for {
// Wait for a packet to be delivered to us through the TUN/TAP adapter
n, err := tun.iface.Read(recvd)
@ -282,20 +300,7 @@ func (tun *TunAdapter) reader() error {
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]...)
bs := append(util.GetBytes(), recvd[:n]...)
toWorker <- bs
}
}