2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2020-05-02 08:08:26 +00:00
|
|
|
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
|
2018-05-03 13:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
package device
|
2017-06-24 13:34:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2020-06-22 19:58:01 +00:00
|
|
|
"encoding/binary"
|
2017-06-24 13:34:17 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2017-06-26 20:07:29 +00:00
|
|
|
type IndexTableEntry struct {
|
|
|
|
peer *Peer
|
|
|
|
handshake *Handshake
|
2018-05-13 16:23:40 +00:00
|
|
|
keypair *Keypair
|
2017-06-26 20:07:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 13:34:17 +00:00
|
|
|
type IndexTable struct {
|
2019-01-03 18:04:00 +00:00
|
|
|
sync.RWMutex
|
2017-06-26 20:07:29 +00:00
|
|
|
table map[uint32]IndexTableEntry
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func randUint32() (uint32, error) {
|
2018-05-13 16:23:40 +00:00
|
|
|
var integer [4]byte
|
|
|
|
_, err := rand.Read(integer[:])
|
2020-06-22 19:58:01 +00:00
|
|
|
// Arbitrary endianness; both are intrinsified by the Go compiler.
|
|
|
|
return binary.LittleEndian.Uint32(integer[:]), err
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (table *IndexTable) Init() {
|
2019-01-03 18:04:00 +00:00
|
|
|
table.Lock()
|
|
|
|
defer table.Unlock()
|
2017-06-26 20:07:29 +00:00
|
|
|
table.table = make(map[uint32]IndexTableEntry)
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 12:41:08 +00:00
|
|
|
func (table *IndexTable) Delete(index uint32) {
|
2019-01-03 18:04:00 +00:00
|
|
|
table.Lock()
|
|
|
|
defer table.Unlock()
|
2017-06-26 20:07:29 +00:00
|
|
|
delete(table.table, index)
|
|
|
|
}
|
|
|
|
|
2018-05-13 16:23:40 +00:00
|
|
|
func (table *IndexTable) SwapIndexForKeypair(index uint32, keypair *Keypair) {
|
2019-01-03 18:04:00 +00:00
|
|
|
table.Lock()
|
|
|
|
defer table.Unlock()
|
2018-05-13 16:23:40 +00:00
|
|
|
entry, ok := table.table[index]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
table.table[index] = IndexTableEntry{
|
|
|
|
peer: entry.peer,
|
|
|
|
keypair: keypair,
|
|
|
|
handshake: nil,
|
|
|
|
}
|
2017-06-26 20:07:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 16:23:40 +00:00
|
|
|
func (table *IndexTable) NewIndexForHandshake(peer *Peer, handshake *Handshake) (uint32, error) {
|
2017-06-24 13:34:17 +00:00
|
|
|
for {
|
|
|
|
// generate random index
|
|
|
|
|
2017-06-26 20:07:29 +00:00
|
|
|
index, err := randUint32()
|
2017-06-24 13:34:17 +00:00
|
|
|
if err != nil {
|
2017-06-26 20:07:29 +00:00
|
|
|
return index, err
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if index used
|
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
table.RLock()
|
2017-06-26 20:07:29 +00:00
|
|
|
_, ok := table.table[index]
|
2019-01-03 18:04:00 +00:00
|
|
|
table.RUnlock()
|
2017-06-24 13:34:17 +00:00
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-13 16:23:40 +00:00
|
|
|
// check again while locked
|
2017-06-24 13:34:17 +00:00
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
table.Lock()
|
2017-06-26 20:07:29 +00:00
|
|
|
_, found := table.table[index]
|
|
|
|
if found {
|
2019-01-03 18:04:00 +00:00
|
|
|
table.Unlock()
|
2017-06-26 20:07:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
table.table[index] = IndexTableEntry{
|
|
|
|
peer: peer,
|
2018-05-13 16:23:40 +00:00
|
|
|
handshake: handshake,
|
|
|
|
keypair: nil,
|
2017-06-26 20:07:29 +00:00
|
|
|
}
|
2019-01-03 18:04:00 +00:00
|
|
|
table.Unlock()
|
2017-06-26 20:07:29 +00:00
|
|
|
return index, nil
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 20:07:29 +00:00
|
|
|
func (table *IndexTable) Lookup(id uint32) IndexTableEntry {
|
2019-01-03 18:04:00 +00:00
|
|
|
table.RLock()
|
|
|
|
defer table.RUnlock()
|
2017-06-26 20:07:29 +00:00
|
|
|
return table.table[id]
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|