-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
56 lines (49 loc) · 1.31 KB
/
header.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
package main
import (
"encoding/binary"
"errors"
)
type sntpHeader struct {
LI,
VN,
Mode,
Stratum,
Poll,
Precision uint8
Delay,
Dispersion,
ReferenceID uint32
ReferenceTimestamp,
OriginateTimestamp,
ReceiveTimestamp,
TransmitTimestamp uint64
}
func parseHeader(header []byte) (sntpHeader, error) {
if len(header) < 48 {
return sntpHeader{}, errors.New("Header was too short")
}
vn := readBits(header[0], 2, 3)
mode := readBits(header[0], 5, 3)
poll := uint8(header[2])
trans := binary.BigEndian.Uint64(header[40:48])
return sntpHeader{
VN: vn, Mode: mode, Poll: poll, TransmitTimestamp: trans,
}, nil
}
func (h *sntpHeader) toBinary() []byte {
result := make([]byte, 48)
writeBits(&result[0], 0, 2, h.LI)
writeBits(&result[0], 2, 3, h.VN)
writeBits(&result[0], 5, 3, h.Mode)
result[1] = byte(h.Stratum)
result[2] = byte(h.Poll)
result[3] = byte(h.Precision)
binary.BigEndian.PutUint32(result[4:8], h.Delay)
binary.BigEndian.PutUint32(result[8:12], h.Dispersion)
binary.BigEndian.PutUint32(result[12:16], h.ReferenceID)
binary.BigEndian.PutUint64(result[16:24], h.ReferenceTimestamp)
binary.BigEndian.PutUint64(result[24:32], h.OriginateTimestamp)
binary.BigEndian.PutUint64(result[32:40], h.ReceiveTimestamp)
binary.BigEndian.PutUint64(result[40:48], h.TransmitTimestamp)
return result
}