Merge pull request #475 from Arceliar/misc

Misc
This commit is contained in:
Neil Alexander 2019-07-29 20:24:49 +01:00 committed by GitHub
commit 853054eb62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 83 additions and 136 deletions

View file

@ -110,20 +110,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
}
// 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
@ -137,8 +127,7 @@ func (tun *TunAdapter) reader() error {
}
// Offset the buffer from now on so that we can ignore ethernet frames if
// they are present
bs := recvd[offset : offset+n]
n -= offset
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
@ -150,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
@ -159,6 +150,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,12 +232,11 @@ 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]...)
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:]
@ -283,12 +274,33 @@ 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]...)
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)
if err != nil {
if !tun.isOpen {
return err
}
panic(err)
}
if n == 0 {
continue
}
bs := append(util.GetBytes(), recvd[:n]...)
toWorker <- bs
}
}