mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-12-23 15:45:40 +00:00
move some logic from TunAdapter.reader into a new function, TunAdapter.readerPacketHandler
This commit is contained in:
parent
38e1503b28
commit
406e143f7f
@ -110,12 +110,24 @@ func (tun *TunAdapter) writer() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *TunAdapter) reader() error {
|
// Run in a separate goroutine by the reader
|
||||||
recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
|
// Does all of the per-packet ICMP checks, passes packets to the right Conn worker
|
||||||
toWorker := make(chan []byte, 32)
|
func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
|
||||||
defer close(toWorker)
|
for recvd := range ch {
|
||||||
go func() {
|
// If it's a TAP adapter, update the buffer slice so that we no longer
|
||||||
for bs := range toWorker {
|
// 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 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
|
||||||
@ -127,6 +139,8 @@ func (tun *TunAdapter) reader() error {
|
|||||||
continue
|
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
|
// 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
|
// and node IDs are. We will need these in order to work out where to send
|
||||||
// the packet
|
// the packet
|
||||||
@ -218,12 +232,11 @@ 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 := 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()
|
||||||
_, known := tun.dials[*dstNodeID]
|
_, 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 {
|
for len(tun.dials[*dstNodeID]) > 32 {
|
||||||
util.PutBytes(tun.dials[*dstNodeID][0])
|
util.PutBytes(tun.dials[*dstNodeID][0])
|
||||||
tun.dials[*dstNodeID] = tun.dials[*dstNodeID][1:]
|
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 we have a connection now, try writing to it
|
||||||
if isIn && session != nil {
|
if isIn && session != nil {
|
||||||
packet := bs
|
|
||||||
select {
|
select {
|
||||||
case session.send <- packet:
|
case session.send <- bs:
|
||||||
default:
|
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 {
|
for {
|
||||||
// Wait for a packet to be delivered to us through the TUN/TAP adapter
|
// Wait for a packet to be delivered to us through the TUN/TAP adapter
|
||||||
n, err := tun.iface.Read(recvd)
|
n, err := tun.iface.Read(recvd)
|
||||||
@ -282,20 +300,7 @@ func (tun *TunAdapter) reader() error {
|
|||||||
if n == 0 {
|
if n == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// If it's a TAP adapter, update the buffer slice so that we no longer
|
bs := append(util.GetBytes(), recvd[:n]...)
|
||||||
// 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
|
toWorker <- bs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user