aboutsummaryrefslogtreecommitdiffstats
path: root/indextable.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--indextable.go (renamed from index.go)47
1 files changed, 22 insertions, 25 deletions
diff --git a/index.go b/indextable.go
index 4a78d55..2d947cd 100644
--- a/index.go
+++ b/indextable.go
@@ -7,18 +7,14 @@ package main
import (
"crypto/rand"
- "encoding/binary"
"sync"
+ "unsafe"
)
-/* Index=0 is reserved for unset indecies
- *
- */
-
type IndexTableEntry struct {
peer *Peer
handshake *Handshake
- keyPair *Keypair
+ keypair *Keypair
}
type IndexTable struct {
@@ -27,34 +23,38 @@ type IndexTable struct {
}
func randUint32() (uint32, error) {
- var buff [4]byte
- _, err := rand.Read(buff[:])
- value := binary.LittleEndian.Uint32(buff[:])
- return value, err
+ var integer [4]byte
+ _, err := rand.Read(integer[:])
+ return *(*uint32)(unsafe.Pointer(&integer[0])), err
}
func (table *IndexTable) Init() {
table.mutex.Lock()
+ defer table.mutex.Unlock()
table.table = make(map[uint32]IndexTableEntry)
- table.mutex.Unlock()
}
func (table *IndexTable) Delete(index uint32) {
- if index == 0 {
- return
- }
table.mutex.Lock()
+ defer table.mutex.Unlock()
delete(table.table, index)
- table.mutex.Unlock()
}
-func (table *IndexTable) Insert(key uint32, value IndexTableEntry) {
+func (table *IndexTable) SwapIndexForKeypair(index uint32, keypair *Keypair) {
table.mutex.Lock()
- table.table[key] = value
- table.mutex.Unlock()
+ defer table.mutex.Unlock()
+ entry, ok := table.table[index]
+ if !ok {
+ return
+ }
+ table.table[index] = IndexTableEntry{
+ peer: entry.peer,
+ keypair: keypair,
+ handshake: nil,
+ }
}
-func (table *IndexTable) NewIndex(peer *Peer) (uint32, error) {
+func (table *IndexTable) NewIndexForHandshake(peer *Peer, handshake *Handshake) (uint32, error) {
for {
// generate random index
@@ -62,9 +62,6 @@ func (table *IndexTable) NewIndex(peer *Peer) (uint32, error) {
if err != nil {
return index, err
}
- if index == 0 {
- continue
- }
// check if index used
@@ -75,7 +72,7 @@ func (table *IndexTable) NewIndex(peer *Peer) (uint32, error) {
continue
}
- // map index to handshake
+ // check again while locked
table.mutex.Lock()
_, found := table.table[index]
@@ -85,8 +82,8 @@ func (table *IndexTable) NewIndex(peer *Peer) (uint32, error) {
}
table.table[index] = IndexTableEntry{
peer: peer,
- handshake: &peer.handshake,
- keyPair: nil,
+ handshake: handshake,
+ keypair: nil,
}
table.mutex.Unlock()
return index, nil