summaryrefslogtreecommitdiff
path: root/src/peer.go
blob: f6eb555ff6ade422198267698257a47b70039b71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
	"net"
	"sync"
	"time"
)

type Peer struct {
	mutex                       sync.RWMutex
	endpointIP                  net.IP        //
	endpointPort                uint16        //
	persistentKeepaliveInterval time.Duration // 0 = disabled
	handshake                   Handshake
	device                      *Device
}

func (device *Device) NewPeer(pk NoisePublicKey) *Peer {
	var peer Peer

	// map public key

	device.mutex.Lock()
	device.peers[pk] = &peer
	device.mutex.Unlock()

	// precompute

	peer.mutex.Lock()
	peer.device = device
	func(h *Handshake) {
		h.mutex.Lock()
		h.remoteStatic = pk
		h.precomputedStaticStatic = device.privateKey.sharedSecret(h.remoteStatic)
		h.mutex.Unlock()
	}(&peer.handshake)
	peer.mutex.Unlock()

	return &peer
}