aboutsummaryrefslogtreecommitdiffstats
path: root/index.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-13 18:23:40 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-13 18:26:09 +0200
commit2c27ab205c992d3387574aa6d57780744d35d36f (patch)
treee95bb88db16bac5d2050d3db13cd28570f44cd72 /index.go
parentRewrite timers and related state machines (diff)
downloadwireguard-go-2c27ab205c992d3387574aa6d57780744d35d36f.tar.xz
wireguard-go-2c27ab205c992d3387574aa6d57780744d35d36f.zip
Rework index hashtable
Diffstat (limited to 'index.go')
-rw-r--r--index.go100
1 files changed, 0 insertions, 100 deletions
diff --git a/index.go b/index.go
deleted file mode 100644
index 4a78d55..0000000
--- a/index.go
+++ /dev/null
@@ -1,100 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0
- *
- * Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
- */
-
-package main
-
-import (
- "crypto/rand"
- "encoding/binary"
- "sync"
-)
-
-/* Index=0 is reserved for unset indecies
- *
- */
-
-type IndexTableEntry struct {
- peer *Peer
- handshake *Handshake
- keyPair *Keypair
-}
-
-type IndexTable struct {
- mutex sync.RWMutex
- table map[uint32]IndexTableEntry
-}
-
-func randUint32() (uint32, error) {
- var buff [4]byte
- _, err := rand.Read(buff[:])
- value := binary.LittleEndian.Uint32(buff[:])
- return value, err
-}
-
-func (table *IndexTable) Init() {
- table.mutex.Lock()
- table.table = make(map[uint32]IndexTableEntry)
- table.mutex.Unlock()
-}
-
-func (table *IndexTable) Delete(index uint32) {
- if index == 0 {
- return
- }
- table.mutex.Lock()
- delete(table.table, index)
- table.mutex.Unlock()
-}
-
-func (table *IndexTable) Insert(key uint32, value IndexTableEntry) {
- table.mutex.Lock()
- table.table[key] = value
- table.mutex.Unlock()
-}
-
-func (table *IndexTable) NewIndex(peer *Peer) (uint32, error) {
- for {
- // generate random index
-
- index, err := randUint32()
- if err != nil {
- return index, err
- }
- if index == 0 {
- continue
- }
-
- // check if index used
-
- table.mutex.RLock()
- _, ok := table.table[index]
- table.mutex.RUnlock()
- if ok {
- continue
- }
-
- // map index to handshake
-
- 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
- }
-}
-
-func (table *IndexTable) Lookup(id uint32) IndexTableEntry {
- table.mutex.RLock()
- defer table.mutex.RUnlock()
- return table.table[id]
-}