-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrecord.go
67 lines (58 loc) · 1.24 KB
/
record.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
package dissector
import (
"bytes"
"encoding/binary"
"errors"
"io"
)
const (
RecordHeaderLen = 5
)
// record content type
const (
ChangeCipherSpec = 0x14
EncryptedAlert = 0x15
Handshake = 0x16
AppData = 0x17
Heartbeat = 0x18
)
var (
ErrBadType = errors.New("bad type")
ErrAlert = errors.New("alert")
)
type Version uint16
type Record struct {
Type uint8
Version Version
Opaque []byte
}
func ReadRecord(r io.Reader) (*Record, error) {
record := &Record{}
if _, err := record.ReadFrom(r); err != nil {
return nil, err
}
return record, nil
}
func (rec *Record) ReadFrom(r io.Reader) (n int64, err error) {
b := make([]byte, RecordHeaderLen)
nn, err := io.ReadFull(r, b)
n += int64(nn)
if err != nil {
return
}
rec.Type = b[0]
rec.Version = Version(binary.BigEndian.Uint16(b[1:3]))
length := int(binary.BigEndian.Uint16(b[3:5]))
rec.Opaque = make([]byte, length)
nn, err = io.ReadFull(r, rec.Opaque)
n += int64(nn)
return
}
func (rec *Record) WriteTo(w io.Writer) (n int64, err error) {
buf := &bytes.Buffer{}
buf.WriteByte(rec.Type)
binary.Write(buf, binary.BigEndian, rec.Version)
binary.Write(buf, binary.BigEndian, uint16(len(rec.Opaque)))
buf.Write(rec.Opaque)
return buf.WriteTo(w)
}