This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handshake_fragment_list.go
83 lines (76 loc) · 2.11 KB
/
handshake_fragment_list.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
package dtls
import (
"errors"
)
type handshakeFragmentList struct {
MsgType handshakeType
Length uint32
MessageSeq uint16
Fragments []*handshake
}
func newHandshakeFragmentList(h *handshake) *handshakeFragmentList {
return &handshakeFragmentList{
MsgType: h.MsgType,
Length: h.Length,
MessageSeq: h.MessageSeq,
Fragments: []*handshake{h},
}
}
func (hfl *handshakeFragmentList) InsertFragment(newHandshake *handshake) error {
if newHandshake.MsgType != hfl.MsgType ||
newHandshake.Length != hfl.Length ||
newHandshake.MessageSeq != hfl.MessageSeq {
return errors.New("Received a handshake fragment which is incompatible with previous fragments")
}
for i, handshake := range hfl.Fragments {
if handshake.FragmentOffset > newHandshake.FragmentOffset {
hfl.InsertFragmentAt(newHandshake, i)
return nil
}
}
hfl.InsertFragmentAt(newHandshake, len(hfl.Fragments))
return nil
}
func (hfl *handshakeFragmentList) InsertFragmentAt(f *handshake, i int) {
hfl.Fragments = append(hfl.Fragments, nil)
copy(hfl.Fragments[i+1:], hfl.Fragments[i:])
hfl.Fragments[i] = f
}
func (hfl *handshakeFragmentList) IsComplete() bool {
if len(hfl.Fragments) == 1 &&
hfl.Fragments[0].FragmentOffset == 0 &&
hfl.Fragments[0].FragmentLength == hfl.Fragments[0].Length {
return true
}
offset := uint32(0)
for _, handshake := range hfl.Fragments {
if handshake.FragmentOffset <= offset {
offset = handshake.FragmentOffset + handshake.FragmentLength
} else {
return false
}
}
if offset == hfl.Length {
return true
}
return false
}
func (hfl *handshakeFragmentList) GetCompleteHandshake() *handshake {
if len(hfl.Fragments) == 1 &&
hfl.Fragments[0].FragmentOffset == 0 &&
hfl.Fragments[0].FragmentLength == hfl.Fragments[0].Length {
return hfl.Fragments[0]
}
h := &handshake{
MsgType: hfl.MsgType,
Length: hfl.Length,
MessageSeq: hfl.MessageSeq,
FragmentOffset: 0,
FragmentLength: hfl.Length,
Fragment: make([]byte, hfl.Length),
}
for _, handshake := range hfl.Fragments {
copy(h.Fragment[handshake.FragmentOffset:], handshake.Fragment)
}
return h
}