This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
131 lines (105 loc) · 2.58 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
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
package lrpc
import (
"bytes"
"encoding/binary"
"github.com/centrify/platform-go-sdk/internal/logging"
)
// type lrpc2HeaderV4 process LRPC2 V4 message header
type lrpc2HeaderV4 struct {
magicNum uint32
headerLen uint16
version uint32
pid uint64
sequenceNum uint32
timestamp uint64
msgDataLen uint32
}
func (h *lrpc2HeaderV4) decodeHeader(buf *bytes.Buffer) error {
var err error
err = binary.Read(buf, lrpc2ByteOrder, &h.magicNum)
if err != nil {
logging.Debugf("Failed to read magic number from header: %v", err)
return err
}
err = binary.Read(buf, lrpc2ByteOrder, &h.headerLen)
if err != nil {
logging.Debugf("Failed to read header length from header: %v", err)
return err
}
err = binary.Read(buf, lrpc2ByteOrder, &h.version)
if err != nil {
logging.Debugf("Failed to read version from header: %v", err)
return err
}
err = binary.Read(buf, lrpc2ByteOrder, &h.pid)
if err != nil {
logging.Debugf("Failed to read pid from header: %v", err)
return err
}
err = binary.Read(buf, lrpc2ByteOrder, &h.sequenceNum)
if err != nil {
logging.Debugf("Failed to read sequence number from header: %v", err)
return err
}
err = binary.Read(buf, lrpc2ByteOrder, &h.timestamp)
if err != nil {
logging.Debugf("Failed to read timestamp from header: %v", err)
return err
}
err = binary.Read(buf, lrpc2ByteOrder, &h.msgDataLen)
if err != nil {
logging.Debugf("Failed to read message data length from header: %v", err)
return err
}
return nil
}
func (h *lrpc2HeaderV4) encodeHeader(buf *bytes.Buffer) error {
var err error
err = binary.Write(buf, lrpc2ByteOrder, h.magicNum)
if err != nil {
return err
}
err = binary.Write(buf, lrpc2ByteOrder, h.headerLen)
if err != nil {
return err
}
err = binary.Write(buf, lrpc2ByteOrder, h.version)
if err != nil {
return err
}
err = binary.Write(buf, lrpc2ByteOrder, h.pid)
if err != nil {
return err
}
err = binary.Write(buf, lrpc2ByteOrder, h.sequenceNum)
if err != nil {
return err
}
err = binary.Write(buf, lrpc2ByteOrder, h.timestamp)
if err != nil {
return err
}
err = binary.Write(buf, lrpc2ByteOrder, h.msgDataLen)
if err != nil {
return err
}
return nil
}
func (h *lrpc2HeaderV4) verifyHeader() error {
if h.magicNum != lrpc2MagicNum {
return ErrLrpc2BadMagicNum
}
if h.headerLen != lrpc2HeaderLengthV4 {
return ErrLrpc2BadHeaderLen
}
if h.version != lrpc2Version4 {
return ErrLrpc2MsgVerMismatch
}
if h.msgDataLen > lrpc2MaxMsgLen {
return ErrLrpc2BadMsgLen
}
return nil
}
func (h *lrpc2HeaderV4) HeaderLen() int {
return int(lrpc2HeaderLengthV4)
}