-
Notifications
You must be signed in to change notification settings - Fork 6
/
peer.go
151 lines (129 loc) · 3.12 KB
/
peer.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package dtls
import (
"crypto/x509"
"encoding/hex"
"errors"
"sync"
"time"
"unicode/utf8"
)
type Peer struct {
transport TransportEndpoint
session *session
activity time.Time
queue chan []byte
mux sync.Mutex
name string
transportQueue chan []byte
processor bool
}
func (p *Peer) SetName(name string) {
p.name = name
}
func (p *Peer) UseQueue(en bool) {
if en {
p.queue = make(chan []byte, 128)
} else {
if p.queue != nil {
q := p.queue
p.queue = nil
close(q)
}
}
}
func (p *Peer) RemoteAddr() string {
if p == nil {
return ""
}
return p.transport.String()
}
func (p *Peer) CipherSuite() string {
return p.session.selectedCipherSuite.String()
}
func (p *Peer) SessionCid() []byte {
return p.session.cid
}
func (p *Peer) SessionPeerCid() []byte {
return p.session.peerCid
}
func (p *Peer) SessionIdentity() []byte {
return p.session.peerIdentity
}
func (p *Peer) SessionIdentityString() string {
if utf8.Valid(p.session.peerIdentity) {
return string(p.session.peerIdentity)
} else {
return hex.EncodeToString(p.session.peerIdentity)
}
}
func (p *Peer) SessionPublicKey() []byte {
if p.session.peerPublicKey != nil && len(p.session.peerPublicKey) != 0 {
return p.session.peerPublicKey
}
return nil
}
func (p *Peer) SessionIdentityOrPublicKeyString() string {
if len(p.session.peerIdentity) != 0 {
if utf8.Valid(p.session.peerIdentity) {
return string(p.session.peerIdentity)
} else {
return hex.EncodeToString(p.session.peerIdentity)
}
} else if len(p.session.peerPublicKey) != 0 {
return hex.EncodeToString(p.session.peerPublicKey)
} else {
return ""
}
}
func (p *Peer) SessionCertificate() *x509.Certificate {
if p.session.peerCert != nil {
return p.session.peerCert
}
return nil
}
func (p *Peer) LastActivity() time.Time {
return p.activity
}
func (p *Peer) touch() time.Time {
prev := p.activity
p.activity = time.Now()
return prev
}
func (p *Peer) Close(alertDesc uint8) {
rec := newRecord(ContentType_Alert, p.session.getEpoch(), p.session.getNextSequence(), nil, newAlert(AlertType_Fatal, alertDesc).Bytes())
p.activity = time.Now()
_ = p.session.writeRecord(rec)
}
func (p *Peer) Write(data []byte) error {
p.activity = time.Now()
rec := newRecord(ContentType_Appdata, p.session.getEpoch(), p.session.getNextSequence(), p.session.peerCid, data)
return p.session.writeRecord(rec)
}
func (p *Peer) Read(timeout time.Duration) ([]byte, error) {
if p.queue == nil {
return nil, errors.New("dtls: transport not in queue mode")
}
select {
case b := <-p.queue:
p.activity = time.Now()
return b, nil
case <-time.After(timeout):
return nil, errors.New("dtls: queued read timed out")
}
}
func (p *Peer) SessionExport() string {
if p.session != nil && p.session.isHandshakeDone() {
return p.session.export()
} else {
return ""
}
}
func (p *Peer) Lock() {
p.mux.Lock()
}
func (p *Peer) Unlock() {
p.mux.Unlock()
}