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