Improved cookie/mac computation code

This commit is contained in:
Mathias Hall-Andersen
2017-08-14 17:09:25 +02:00
parent a4eff12d7f
commit 12e8db2066
10 changed files with 523 additions and 442 deletions

View File

@@ -272,7 +272,9 @@ func (device *Device) RoutineHandshake() {
case MessageCookieReplyType:
// verify and update peer cookie state
// unmarshal packet
logDebug.Println("Process cookie reply from:", elem.source.String())
var reply MessageCookieReply
reader := bytes.NewReader(elem.packet)
@@ -281,7 +283,14 @@ func (device *Device) RoutineHandshake() {
logDebug.Println("Failed to decode cookie reply")
return
}
device.ConsumeMessageCookieReply(&reply)
// lookup peer and consume response
entry := device.indices.Lookup(reply.Receiver)
if entry.peer == nil {
return
}
entry.peer.mac.ConsumeReply(&reply)
continue
case MessageInitiationType, MessageResponseType:
@@ -298,12 +307,17 @@ func (device *Device) RoutineHandshake() {
// construct cookie reply
logDebug.Println("Sending cookie reply to:", elem.source.String())
sender := binary.LittleEndian.Uint32(elem.packet[4:8]) // "sender" always follows "type"
reply, err := device.CreateMessageCookieReply(elem.packet, sender, elem.source)
reply, err := device.mac.CreateReply(elem.packet, sender, elem.source)
if err != nil {
logError.Println("Failed to create cookie reply:", err)
return
}
// marshal and send reply
writer := bytes.NewBuffer(temp[:0])
binary.Write(writer, binary.LittleEndian, reply)
_, err = device.net.conn.WriteToUDP(
@@ -392,6 +406,8 @@ func (device *Device) RoutineHandshake() {
case MessageResponseType:
logDebug.Println("Process response")
// unmarshal
var msg MessageResponse