Debian Codegraph @

dtls.go

raw symbols SCIP
1 package mint
2
3 import (
4 "fmt"
5 "github.com/bifurcation/mint/syntax"
6 "time"
7 )
8
9 const (
10 initialMtu = 1200
11 initialTimeout = 100
12 )
13
14 // labels for timers
15 const (
16 retransmitTimerLabel = "handshake retransmit"
17 ackTimerLabel = "ack timer"
18 )
19
20 type SentHandshakeFragment struct {
21 seq uint32
22 offset int
23 fragLength int
24 record uint64
25 acked bool
26 }
27
28 type DtlsAck struct {
29 RecordNumbers []uint64 `tls:"head=2"`
30 }
31
32 func wireVersion(h *HandshakeLayer) uint16 {
33 if h.datagram {
34 return dtls12WireVersion
35 }
36 return tls12Version
37 }
38
39 func dtlsConvertVersion(version uint16) uint16 {
40 if version == tls12Version {
41 return dtls12WireVersion
42 }
43 if version == tls10Version {
44 return 0xfeff
45 }
46 panic(fmt.Sprintf("Internal error, unexpected version=%d", version))
47 }
48
49 // TODO([email protected]): Move these to state-machine.go
50 func (h *HandshakeContext) handshakeRetransmit() error {
51 if _, err := h.hOut.SendQueuedMessages(); err != nil {
52 return err
53 }
54
55 h.timers.start(retransmitTimerLabel,
56 h.handshakeRetransmit,
57 h.timeoutMS)
58
59 // TODO([email protected]): Back off timer
60 return nil
61 }
62
63 func (h *HandshakeContext) sendAck() error {
64 toack := h.hIn.recvdRecords
65
66 count := (initialMtu - 2) / 8 // TODO([email protected]): Current MTU
67 if len(toack) > count {
68 toack = toack[:count]
69 }
70 logf(logTypeHandshake, "Sending ACK: [%x]", toack)
71
72 ack := &DtlsAck{toack}
73 body, err := syntax.Marshal(&ack)
74 if err != nil {
75 return err
76 }
77 err = h.hOut.conn.WriteRecord(&TLSPlaintext{
78 contentType: RecordTypeAck,
79 fragment: body,
80 })
81 if err != nil {
82 return err
83 }
84 return nil
85 }
86
87 func (h *HandshakeContext) processAck(data []byte) error {
88 // Cancel the retransmit timer because we will be resending
89 // and possibly re-arming later.
90 h.timers.cancel(retransmitTimerLabel)
91
92 ack := &DtlsAck{}
93 read, err := syntax.Unmarshal(data, &ack)
94 if err != nil {
95 return err
96 }
97 if len(data) != read {
98 return fmt.Errorf("Invalid encoding: Extra data not consumed")
99 }
100 logf(logTypeHandshake, "ACK: [%x]", ack.RecordNumbers)
101
102 for _, r := range ack.RecordNumbers {
103 for _, m := range h.sentFragments {
104 if r == m.record {
105 logf(logTypeHandshake, "Marking %v %v(%v) as acked",
106 m.seq, m.offset, m.fragLength)
107 m.acked = true
108 }
109 }
110 }
111
112 count, err := h.hOut.SendQueuedMessages()
113 if err != nil {
114 return err
115 }
116
117 if count == 0 {
118 logf(logTypeHandshake, "All messages ACKed")
119 h.hOut.ClearQueuedMessages()
120 return nil
121 }
122
123 // Reset the timer
124 h.timers.start(retransmitTimerLabel,
125 h.handshakeRetransmit,
126 h.timeoutMS)
127
128 return nil
129 }
130
131 func (c *Conn) GetDTLSTimeout() (bool, time.Duration) {
132 return c.hsCtx.timers.remaining()
133 }
134
135 func (h *HandshakeContext) receivedHandshakeMessage() {
136 logf(logTypeHandshake, "%p Received handshake, waiting for start of flight = %v", h, h.waitingNextFlight)
137 // This just enables tests.
138 if h.hIn == nil {
139 return
140 }
141
142 if !h.hIn.datagram {
143 return
144 }
145
146 if h.waitingNextFlight {
147 logf(logTypeHandshake, "Received the start of the flight")
148
149 // Clear the outgoing DTLS queue and terminate the retransmit timer
150 h.hOut.ClearQueuedMessages()
151 h.timers.cancel(retransmitTimerLabel)
152
153 // OK, we're not waiting any more.
154 h.waitingNextFlight = false
155 }
156
157 // Now pre-emptively arm the ACK timer if it's not armed already.
158 // We'll automatically dis-arm it at the end of the handshake.
159 if h.timers.getTimer(ackTimerLabel) == nil {
160 h.timers.start(ackTimerLabel, h.sendAck, h.timeoutMS/4)
161 }
162 }
163
164 func (h *HandshakeContext) receivedEndOfFlight() {
165 logf(logTypeHandshake, "%p Received the end of the flight", h)
166 if !h.hIn.datagram {
167 return
168 }
169
170 // Empty incoming queue
171 h.hIn.queued = nil
172
173 // Note that we are waiting for the next flight.
174 h.waitingNextFlight = true
175
176 // Clear the ACK queue.
177 h.hIn.recvdRecords = nil
178
179 // Disarm the ACK timer
180 h.timers.cancel(ackTimerLabel)
181 }
182
183 func (h *HandshakeContext) receivedFinalFlight() {
184 logf(logTypeHandshake, "%p Received final flight", h)
185 if !h.hIn.datagram {
186 return
187 }
188
189 // Disarm the ACK timer
190 h.timers.cancel(ackTimerLabel)
191
192 // But send an ACK immediately.
193 h.sendAck()
194 }
195
196 func (h *HandshakeContext) fragmentAcked(seq uint32, offset int, fraglen int) bool {
197 logf(logTypeHandshake, "Looking to see if fragment %v %v(%v) was acked", seq, offset, fraglen)
198 for _, f := range h.sentFragments {
199 if !f.acked {
200 continue
201 }
202
203 if f.seq != seq {
204 continue
205 }
206
207 if f.offset > offset {
208 continue
209 }
210
211 // At this point, we know that the stored fragment starts
212 // at or before what we want to send, so check where the end
213 // is.
214 if f.offset+f.fragLength < offset+fraglen {
215 continue
216 }
217
218 return true
219 }
220
221 return false
222 }