Fixed receive path infinite loop

This commit is contained in:
Mathias Hall-Andersen 2017-12-01 00:03:06 +01:00
parent cd369cde45
commit cb09125dc4

View file

@ -98,118 +98,115 @@ func (device *Device) RoutineReceiveIncoming(IP int, bind Bind) {
logDebug := device.log.Debug logDebug := device.log.Debug
logDebug.Println("Routine, receive incoming, IP version:", IP) logDebug.Println("Routine, receive incoming, IP version:", IP)
// receive datagrams until conn is closed
buffer := device.GetMessageBuffer()
var (
err error
size int
endpoint Endpoint
)
for { for {
// receive datagrams until conn is closed // read next datagram
buffer := device.GetMessageBuffer() switch IP {
case ipv4.Version:
size, endpoint, err = bind.ReceiveIPv4(buffer[:])
case ipv6.Version:
size, endpoint, err = bind.ReceiveIPv6(buffer[:])
default:
return
}
var ( if err != nil {
err error return
size int }
endpoint Endpoint
)
for { if size < MinMessageSize {
continue
}
// read next datagram // check size of packet
switch IP { packet := buffer[:size]
case ipv4.Version: msgType := binary.LittleEndian.Uint32(packet[:4])
size, endpoint, err = bind.ReceiveIPv4(buffer[:])
case ipv6.Version:
size, endpoint, err = bind.ReceiveIPv6(buffer[:])
default:
return
}
if err != nil { var okay bool
break
}
if size < MinMessageSize { switch msgType {
// check if transport
case MessageTransportType:
// check size
if len(packet) < MessageTransportType {
continue continue
} }
// check size of packet // lookup key pair
packet := buffer[:size] receiver := binary.LittleEndian.Uint32(
msgType := binary.LittleEndian.Uint32(packet[:4]) packet[MessageTransportOffsetReceiver:MessageTransportOffsetCounter],
)
value := device.indices.Lookup(receiver)
keyPair := value.keyPair
if keyPair == nil {
continue
}
var okay bool // check key-pair expiry
switch msgType { if keyPair.created.Add(RejectAfterTime).Before(time.Now()) {
continue
}
// check if transport // create work element
case MessageTransportType: peer := value.peer
elem := &QueueInboundElement{
packet: packet,
buffer: buffer,
keyPair: keyPair,
dropped: AtomicFalse,
endpoint: endpoint,
}
elem.mutex.Lock()
// check size // add to decryption queues
if len(packet) < MessageTransportType { device.addToDecryptionQueue(device.queue.decryption, elem)
continue device.addToInboundQueue(peer.queue.inbound, elem)
} buffer = device.GetMessageBuffer()
continue
// lookup key pair // otherwise it is a fixed size & handshake related packet
receiver := binary.LittleEndian.Uint32( case MessageInitiationType:
packet[MessageTransportOffsetReceiver:MessageTransportOffsetCounter], okay = len(packet) == MessageInitiationSize
)
value := device.indices.Lookup(receiver)
keyPair := value.keyPair
if keyPair == nil {
continue
}
// check key-pair expiry case MessageResponseType:
okay = len(packet) == MessageResponseSize
if keyPair.created.Add(RejectAfterTime).Before(time.Now()) { case MessageCookieReplyType:
continue okay = len(packet) == MessageCookieReplySize
} }
// create work element if okay {
device.addToHandshakeQueue(
peer := value.peer device.queue.handshake,
elem := &QueueInboundElement{ QueueHandshakeElement{
packet: packet, msgType: msgType,
buffer: buffer, buffer: buffer,
keyPair: keyPair, packet: packet,
dropped: AtomicFalse,
endpoint: endpoint, endpoint: endpoint,
} },
elem.mutex.Lock() )
buffer = device.GetMessageBuffer()
// add to decryption queues
device.addToDecryptionQueue(device.queue.decryption, elem)
device.addToInboundQueue(peer.queue.inbound, elem)
buffer = device.GetMessageBuffer()
continue
// otherwise it is a fixed size & handshake related packet
case MessageInitiationType:
okay = len(packet) == MessageInitiationSize
case MessageResponseType:
okay = len(packet) == MessageResponseSize
case MessageCookieReplyType:
okay = len(packet) == MessageCookieReplySize
}
if okay {
device.addToHandshakeQueue(
device.queue.handshake,
QueueHandshakeElement{
msgType: msgType,
buffer: buffer,
packet: packet,
endpoint: endpoint,
},
)
buffer = device.GetMessageBuffer()
}
} }
} }
} }