2018-05-03 13:04:00 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2017-06-24 13:34:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2017-08-11 14:18:20 +00:00
|
|
|
"encoding/binary"
|
2017-06-24 13:34:17 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Index=0 is reserved for unset indecies
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-06-26 20:07:29 +00:00
|
|
|
type IndexTableEntry struct {
|
|
|
|
peer *Peer
|
|
|
|
handshake *Handshake
|
|
|
|
keyPair *KeyPair
|
|
|
|
}
|
|
|
|
|
2017-06-24 13:34:17 +00:00
|
|
|
type IndexTable struct {
|
2017-06-26 20:07:29 +00:00
|
|
|
mutex sync.RWMutex
|
|
|
|
table map[uint32]IndexTableEntry
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func randUint32() (uint32, error) {
|
|
|
|
var buff [4]byte
|
|
|
|
_, err := rand.Read(buff[:])
|
2017-08-11 14:18:20 +00:00
|
|
|
value := binary.LittleEndian.Uint32(buff[:])
|
|
|
|
return value, err
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (table *IndexTable) Init() {
|
|
|
|
table.mutex.Lock()
|
2017-06-26 20:07:29 +00:00
|
|
|
table.table = make(map[uint32]IndexTableEntry)
|
|
|
|
table.mutex.Unlock()
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 12:41:08 +00:00
|
|
|
func (table *IndexTable) Delete(index uint32) {
|
2017-06-26 20:07:29 +00:00
|
|
|
if index == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
table.mutex.Lock()
|
|
|
|
delete(table.table, index)
|
|
|
|
table.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *IndexTable) Insert(key uint32, value IndexTableEntry) {
|
2017-06-24 13:34:17 +00:00
|
|
|
table.mutex.Lock()
|
2017-06-26 20:07:29 +00:00
|
|
|
table.table[key] = value
|
|
|
|
table.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *IndexTable) NewIndex(peer *Peer) (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
|
|
|
}
|
2017-06-26 20:07:29 +00:00
|
|
|
if index == 0 {
|
2017-06-24 13:34:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if index used
|
|
|
|
|
2017-06-26 20:07:29 +00:00
|
|
|
table.mutex.RLock()
|
|
|
|
_, ok := table.table[index]
|
2017-07-17 14:16:18 +00:00
|
|
|
table.mutex.RUnlock()
|
2017-06-24 13:34:17 +00:00
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-17 14:16:18 +00:00
|
|
|
// map index to handshake
|
2017-06-24 13:34:17 +00:00
|
|
|
|
2017-06-26 20:07:29 +00:00
|
|
|
table.mutex.Lock()
|
|
|
|
_, found := table.table[index]
|
|
|
|
if found {
|
|
|
|
table.mutex.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
table.table[index] = IndexTableEntry{
|
|
|
|
peer: peer,
|
|
|
|
handshake: &peer.handshake,
|
|
|
|
keyPair: nil,
|
|
|
|
}
|
|
|
|
table.mutex.Unlock()
|
|
|
|
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 {
|
2017-06-24 13:34:17 +00:00
|
|
|
table.mutex.RLock()
|
|
|
|
defer table.mutex.RUnlock()
|
2017-06-26 20:07:29 +00:00
|
|
|
return table.table[id]
|
2017-06-24 13:34:17 +00:00
|
|
|
}
|